bozo 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,58 @@
1
+ module Bozo
2
+
3
+ # Module mixed into the all Bozo executors that provides functionality for
4
+ # command execution and logging.
5
+ module Runner
6
+
7
+ include Bozo::Logging
8
+
9
+ # Returns the global parameters.
10
+ attr_accessor :global_params
11
+
12
+ # Returns the command parameters.
13
+ attr_accessor :params
14
+
15
+ # Returns the environment variables.
16
+ attr_accessor :env
17
+
18
+ # Returns the build configuration.
19
+ attr_accessor :build_configuration
20
+
21
+ # Returns whether the build is being run on a build server.
22
+ def build_server?
23
+ global_params[:build_server]
24
+ end
25
+
26
+ # Returns the environment the build is being execute in.
27
+ def environment
28
+ global_params[:environment]
29
+ end
30
+
31
+ # Executes a command line tool.
32
+ #
33
+ # Raises a `Bozo::ExecutionError` if the command responds with a non-zero
34
+ # exit code.
35
+ #
36
+ # @param [Symbol] tool
37
+ # A friendly identifier for the tool.
38
+ # @param [Array] args
39
+ # An array of arguments making up the command to execute.
40
+ def execute_command(tool, args)
41
+ args.flatten!
42
+
43
+ executable = File.basename(args.first)
44
+ arguments = args[1..-1].join(' ')
45
+
46
+ log_info " #{tool} - #{executable} #{arguments}"
47
+
48
+ raise Bozo::ExecutionError.new(tool, args, $?.exitstatus) unless system args.join ' '
49
+ end
50
+
51
+ # Returns the build version.
52
+ def version
53
+ build_configuration.version
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,87 @@
1
+ module Bozo::Versioning
2
+
3
+ class Version
4
+
5
+ # @param major [Integer]
6
+ # @param minor [Integer]
7
+ # @param patch [Integer]
8
+ def initialize(major, minor, patch)
9
+ @major = major
10
+ @minor = minor
11
+ @patch = patch
12
+ end
13
+
14
+ # Loads the version from the VERSION file
15
+ def self.load_from_file
16
+ raise no_version_file unless File.exist? 'VERSION'
17
+ version = File.read('VERSION').strip
18
+ raise no_version if version.empty?
19
+ parse version
20
+ end
21
+
22
+ # Parses a version string to create a Version instance
23
+ #
24
+ # @param version [String]
25
+ def self.parse(version)
26
+ major, minor, patch = *version.split('.').map(&:to_i)
27
+ Version.new major, minor, patch
28
+ end
29
+
30
+ def major
31
+ @major
32
+ end
33
+
34
+ def minor
35
+ @minor
36
+ end
37
+
38
+ def patch
39
+ @patch
40
+ end
41
+
42
+ # Writes the version from the VERSION file
43
+ def write_to_file
44
+ File.open("VERSION", 'w') { |f| f << to_s }
45
+ end
46
+
47
+ # Bumps the part of the version
48
+ #
49
+ # @param part [Symbol]
50
+ def bump(part)
51
+ send "bump_#{part}".to_sym
52
+ end
53
+
54
+ def to_s
55
+ "#{@major}.#{@minor}.#{@patch}"
56
+ end
57
+
58
+ private
59
+
60
+ def bump_major
61
+ @major += 1
62
+ @minor = 0
63
+ @patch = 0
64
+ end
65
+
66
+ def bump_minor
67
+ @minor += 1
68
+ @patch = 0
69
+ end
70
+
71
+ def bump_patch
72
+ @patch += 1
73
+ end
74
+
75
+ # Error if no version has been specified
76
+ def self.no_version
77
+ Bozo::ConfigurationError.new 'No version specified'
78
+ end
79
+
80
+ # Error if the version file could not be found
81
+ def self.no_version_file
82
+ Bozo::ConfigurationError.new "VERSION file could not be found in project #{Dir.pwd}"
83
+ end
84
+
85
+ end
86
+
87
+ end
@@ -0,0 +1,30 @@
1
+ module Bozo::Versioning
2
+
3
+ # Error raised when a runner or hook finds the configuration or build in an
4
+ # unexpected state.
5
+ class VersionBumpError < StandardError
6
+
7
+ # The code the program should exit with upon handling this error.
8
+ attr_reader :exit_code
9
+
10
+ # A message explaining the corrective actions someone can take to avoid the
11
+ # error.
12
+ attr_reader :message
13
+
14
+ # Creates a new instance.
15
+ #
16
+ # @param [String] message
17
+ # A message explaining the corrective actions someone can take to avoid
18
+ # the error.
19
+ def initialize(message)
20
+ @message = message
21
+ @exit_code = -1
22
+ end
23
+
24
+ def inspect
25
+ "Version bump error: #{message}"
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,36 @@
1
+ module Bozo::Versioning
2
+
3
+ class VersionBumper
4
+
5
+ include Bozo::Logging
6
+
7
+ def bump(part)
8
+ unless can_be_bumped?
9
+ raise VersionBumpError.new "State of the repository means the version can't be bumped. Ensure the staging index is empty."
10
+ end
11
+
12
+ version = Version.load_from_file
13
+ original_version = version.to_s
14
+
15
+ version.bump part
16
+ version.write_to_file
17
+
18
+ commit_version original_version, version
19
+
20
+ log_info "Bumped #{original_version} to #{version}"
21
+ end
22
+
23
+ private
24
+
25
+ def can_be_bumped?
26
+ `git diff --cached --name-only`.length == 0
27
+ end
28
+
29
+ def commit_version(original, new)
30
+ `git add VERSION`
31
+ `git commit -m "Bumped #{original} to #{new}."`
32
+ end
33
+
34
+ end
35
+
36
+ end
data/lib/bozo.rb ADDED
@@ -0,0 +1,131 @@
1
+ $:.push File.expand_path(File.dirname(__FILE__))
2
+
3
+ require 'rubygems'
4
+ require 'rainbow'
5
+ require 'rainbow/ext/string'
6
+ require 'gli'
7
+ require 'gli_version'
8
+ require 'bozo/bozo'
9
+ require 'bozo/class_name_helpers'
10
+ require 'bozo/logging'
11
+ require 'bozo/runner'
12
+ require 'bozo/executor'
13
+ require 'bozo/bozo_configuration'
14
+ require 'bozo/configuration_error'
15
+ require 'bozo/execution_error'
16
+ require 'bozo/versioning/version'
17
+ require 'bozo/versioning/version_bumper'
18
+ require 'bozo/versioning/version_bump_error'
19
+
20
+ include GLI
21
+
22
+ # Set the location of the bozo runtime configuration file
23
+ config_file '.bozorc'
24
+
25
+ version Bozo::VERSION
26
+
27
+ desc 'Run as a build server, allows more destructive actions'
28
+ switch 'build-server'
29
+
30
+ default_config_file = 'bozorc.rb'
31
+
32
+ desc "The build configuration file (defaults to #{default_config_file})"
33
+ default_value default_config_file
34
+ arg_name 'path'
35
+ flag 'config-file'
36
+
37
+ default_environment = 'development'
38
+
39
+ desc "The build environment (defaults to #{default_environment})"
40
+ default_value default_environment
41
+ arg_name 'name'
42
+ flag :environment
43
+
44
+ pre do |global_options, command, options, arguments|
45
+ # Load the configuration file.
46
+ config = Bozo::BozoConfiguration.new
47
+ config.load global_options[:'config-file']
48
+
49
+ puts "Building #{config.version} using Bozo #{Bozo::VERSION}"
50
+ puts ''
51
+
52
+ # Initialize the executor.
53
+ @executor = Bozo::Executor.new config, global_options
54
+
55
+ true
56
+ end
57
+
58
+ # Helper method for defining a command that invokes a bozo task.
59
+ #
60
+ # The first provided name will be passed on to bozo.
61
+ #
62
+ # Takes an optional block for defining command-specific flags and switches.
63
+ #
64
+ # @param [Symbol] command_names
65
+ # The names to give the command.
66
+ def bozo_command(*command_names)
67
+ command_name = command_names.first
68
+ command command_names do |c|
69
+ yield c if block_given?
70
+ c.action do |global_options, options, arguments|
71
+ @executor.execute command_name, options, ENV.to_hash
72
+ end
73
+ end
74
+ end
75
+
76
+ desc 'Remove bozo and all its artifacts'
77
+ bozo_command :uninstall
78
+
79
+ desc 'Remove all build artifacts'
80
+ bozo_command :clean
81
+
82
+ desc 'Retrieve all project dependencies'
83
+ bozo_command :dependencies
84
+
85
+ desc 'Prepare the project'
86
+ bozo_command :prepare
87
+
88
+ desc 'Compile the project'
89
+ bozo_command :compile, :c
90
+
91
+ desc 'Run the tests for the project'
92
+ bozo_command :test, :t
93
+
94
+ desc 'Create a package'
95
+ bozo_command :package, :p do |c|
96
+ c.desc 'Force a pre-release package to be created'
97
+ c.switch 'pre-release'
98
+ end
99
+
100
+ desc 'Publish a package'
101
+ bozo_command :publish do |c|
102
+ c.desc 'Force a pre-release package to be published'
103
+ c.switch 'pre-release'
104
+ end
105
+
106
+ desc 'Bump the version number'
107
+ long_desc <<EOS
108
+ Specify either 'major', 'minor' or 'patch' as an argument. For example: `bozo bump major`
109
+ EOS
110
+ command :bump do |c|
111
+ c.action do |global_options, options, args|
112
+ if args.empty?
113
+ puts "Specify either 'major', 'minor' or 'patch' as an argument"
114
+ else
115
+ version_bumper = Bozo::Versioning::VersionBumper.new
116
+ version_bumper.bump args[0].to_sym
117
+ end
118
+ end
119
+ end
120
+
121
+ on_error do |exception|
122
+ puts exception.inspect.color(:red).bright
123
+
124
+ if exception.kind_of? Bozo::ExecutionError or exception.kind_of? Bozo::ConfigurationError
125
+ false
126
+ else
127
+ puts ''
128
+ puts exception.backtrace.join("\n").color(:red)
129
+ true
130
+ end
131
+ end
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ class BozoConfigurationVersion < Test::Unit::TestCase
4
+
5
+ include FileUtils
6
+
7
+ def setup
8
+ @original_path = Dir.pwd
9
+ @temp_dir = File.expand_path File.join(@original_path, 'temp')
10
+ rm_rf @temp_dir if Dir.exist? @temp_dir
11
+ mkdir_p @temp_dir
12
+ Dir.chdir @temp_dir
13
+ end
14
+
15
+ def teardown
16
+ Dir.chdir @original_path
17
+ rm_rf @temp_dir
18
+ end
19
+
20
+ def test_loads_version
21
+ File.open("VERSION", 'w') { |f| f << '1.2.3' }
22
+
23
+ configuration = Bozo::BozoConfiguration.new
24
+ assert_equal configuration.version.to_s, '1.2.3'
25
+ end
26
+
27
+ def test_version_file_does_not_exist
28
+ # ensure the VERSION file doesn't exist
29
+ rm 'VERSION' if File.exist? 'VERSION'
30
+
31
+ configuration = Bozo::BozoConfiguration.new
32
+ assert_raise(Bozo::ConfigurationError) { configuration.version }
33
+ end
34
+
35
+ def test_no_version
36
+ touch 'VERSION'
37
+
38
+ configuration = Bozo::BozoConfiguration.new
39
+ assert_raise(Bozo::ConfigurationError) { configuration.version }
40
+ end
41
+
42
+ end
@@ -0,0 +1,3 @@
1
+ require 'bundler/setup'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/bozo'
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+
3
+ class VersionBumperTests < Test::Unit::TestCase
4
+
5
+ include FileUtils
6
+
7
+ def setup
8
+ @original_path = Dir.pwd
9
+ @temp_dir = File.expand_path File.join(@original_path, 'temp')
10
+ rm_rf @temp_dir if Dir.exist? @temp_dir
11
+ mkdir_p @temp_dir
12
+ Dir.chdir @temp_dir
13
+ end
14
+
15
+ def teardown
16
+ Dir.chdir @original_path
17
+ rm_rf @temp_dir
18
+ end
19
+
20
+ def test_bumps_version_file
21
+ git_init_and_commit_initial
22
+ File.open("VERSION", 'w') { |f| f << '1.1.1' }
23
+
24
+ version_bumper = Bozo::Versioning::VersionBumper.new
25
+ version_bumper.bump :major
26
+
27
+ assert_equal File.read('VERSION').strip, '2.0.0'
28
+ end
29
+
30
+ def test_bump_commits_file
31
+ git_init_and_commit_initial
32
+ File.open("VERSION", 'w') { |f| f << '1.1.1' }
33
+
34
+ version_bumper = Bozo::Versioning::VersionBumper.new
35
+ version_bumper.bump :major
36
+
37
+ assert_true `git log -1 --oneline`.include?('Bumped 1.1.1 to 2.0.0')
38
+ end
39
+
40
+ def test_git_staging_pending_stops_bump
41
+ git_init_and_commit_initial
42
+
43
+ touch 'tempfile2'
44
+ `git add tempfile2` # add file to staging to block bump
45
+
46
+ File.open("VERSION", 'w') { |f| f << '1.1.1' }
47
+
48
+ version_bumper = Bozo::Versioning::VersionBumper.new
49
+
50
+ assert_raise(Bozo::Versioning::VersionBumpError) { version_bumper.bump :major }
51
+ end
52
+
53
+ private
54
+
55
+ def git_init_and_commit_initial
56
+ touch 'tempfile'
57
+ `git init`
58
+ `git add tempfile`
59
+ `git commit -m "initial commit"`
60
+ end
61
+
62
+ end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class VersionTests < Test::Unit::TestCase
4
+
5
+ def test_bumps_major
6
+ version = Bozo::Versioning::Version.parse '1.1.1'
7
+ version.bump :major
8
+
9
+ assert_equal version.to_s, '2.0.0'
10
+ end
11
+
12
+ def test_bumps_minor
13
+ version = Bozo::Versioning::Version.parse '1.1.1'
14
+ version.bump :minor
15
+
16
+ assert_equal version.to_s, '1.2.0'
17
+ end
18
+
19
+ def test_bumps_patch
20
+ version = Bozo::Versioning::Version.parse '1.1.1'
21
+ version.bump :patch
22
+
23
+ assert_equal version.to_s, '1.1.2'
24
+ end
25
+
26
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bozo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garry Shutler
@@ -62,6 +62,24 @@ executables:
62
62
  extensions: []
63
63
  extra_rdoc_files: []
64
64
  files:
65
+ - lib/bozo/bozo.rb
66
+ - lib/bozo/bozo_configuration.rb
67
+ - lib/bozo/class_name_helpers.rb
68
+ - lib/bozo/configuration_error.rb
69
+ - lib/bozo/execution_error.rb
70
+ - lib/bozo/executor.rb
71
+ - lib/bozo/logging.rb
72
+ - lib/bozo/runner.rb
73
+ - lib/bozo/versioning/version.rb
74
+ - lib/bozo/versioning/version_bumper.rb
75
+ - lib/bozo/versioning/version_bump_error.rb
76
+ - lib/bozo.rb
77
+ - README.md
78
+ - VERSION
79
+ - test/bozo_configuration_version_tests.rb
80
+ - test/test_helper.rb
81
+ - test/version_bumper_tests.rb
82
+ - test/version_tests.rb
65
83
  - bin/bozo
66
84
  homepage: https://github.com/zopaUK/bozo
67
85
  licenses: []
@@ -86,4 +104,8 @@ rubygems_version: 2.0.3
86
104
  signing_key:
87
105
  specification_version: 4
88
106
  summary: Zopa build system
89
- test_files: []
107
+ test_files:
108
+ - test/bozo_configuration_version_tests.rb
109
+ - test/test_helper.rb
110
+ - test/version_bumper_tests.rb
111
+ - test/version_tests.rb