Class: YSI::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/yes_ship_it/engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEngine

Returns a new instance of Engine.



10
11
12
13
14
15
# File 'lib/yes_ship_it/engine.rb', line 10

def initialize
  @assertions = []
  @out = STDOUT
  @data_dir = File.expand_path("~/.ysi")
  self.dry_run = false
end

Instance Attribute Details

#assertionsObject (readonly)

Returns the value of attribute assertions.



3
4
5
# File 'lib/yes_ship_it/engine.rb', line 3

def assertions
  @assertions
end

#data_dirObject

Returns the value of attribute data_dir.



8
9
10
# File 'lib/yes_ship_it/engine.rb', line 8

def data_dir
  @data_dir
end

#executorObject (readonly)

Returns the value of attribute executor.



4
5
6
# File 'lib/yes_ship_it/engine.rb', line 4

def executor
  @executor
end

#outObject

Returns the value of attribute out.



7
8
9
# File 'lib/yes_ship_it/engine.rb', line 7

def out
  @out
end

#release_archiveObject

Returns the value of attribute release_archive.



6
7
8
# File 'lib/yes_ship_it/engine.rb', line 6

def release_archive
  @release_archive
end

#tag_dateObject



100
101
102
# File 'lib/yes_ship_it/engine.rb', line 100

def tag_date
  @tag_date && @tag_date.utc
end

#versionObject

Returns the value of attribute version.



6
7
8
# File 'lib/yes_ship_it/engine.rb', line 6

def version
  @version
end

Instance Method Details

#check_assertion(assertion_class) ⇒ Object



68
69
70
# File 'lib/yes_ship_it/engine.rb', line 68

def check_assertion(assertion_class)
  assertion_class.new(self).check
end

#config_urlObject



92
93
94
# File 'lib/yes_ship_it/engine.rb', line 92

def config_url
  "https://raw.githubusercontent.com/#{github_project_name}/master/yes_ship_it.conf"
end

#dependency_errored?(assertion, errored_assertions) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
114
115
116
117
# File 'lib/yes_ship_it/engine.rb', line 108

def dependency_errored?(assertion, errored_assertions)
  assertion.needs.each do |need|
    errored_assertions.each do |errored_assertion|
      if errored_assertion.class == need
        return true
      end
    end
  end
  false
end

#dry_run=(dry_run) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/yes_ship_it/engine.rb', line 17

def dry_run=(dry_run)
  if dry_run
    @executor = DryExecutor.new
  else
    @executor = Executor.new
  end
end

#dry_run?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/yes_ship_it/engine.rb', line 25

def dry_run?
  @executor.is_a?(DryExecutor)
end

#github_project_nameObject



76
77
78
79
80
81
82
# File 'lib/yes_ship_it/engine.rb', line 76

def github_project_name
  if !@github_project_name
    origin = Git.new(Executor.new).origin
    @github_project_name = origin.match("[email protected]:(.*)")[1]
  end
  @github_project_name
end

#parse(config) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/yes_ship_it/engine.rb', line 39

def parse(config)
  config.each do |key,value|
    if key == "include"
      included_file = value
      configs_path = File.expand_path("../../../configs", __FILE__)
      read(File.join(configs_path, included_file + ".conf"))
    elsif key == "assertions"
      assertions = value
      if assertions
        assertions.each do |assertion_name, parameters|
          if assertion_name == "version_number"
            out.puts "Warning: use `version` instead of `version_number`."
            out.puts
            assertion_name = "version"
          end

          assertion = YSI::Assertion.class_for_name(assertion_name).new(self)
          if parameters
            parameters.each do |parameter_name, parameter_value|
              assertion.send(parameter_name + "=", parameter_value)
            end
          end
          @assertions << assertion
        end
      end
    end
  end
end

#project_nameObject



72
73
74
# File 'lib/yes_ship_it/engine.rb', line 72

def project_name
  File.basename(Dir.pwd)
end

#project_urlObject



84
85
86
# File 'lib/yes_ship_it/engine.rb', line 84

def project_url
  "https://github.com/#{github_project_name}"
end

#read(filename) ⇒ Object



34
35
36
37
# File 'lib/yes_ship_it/engine.rb', line 34

def read(filename)
  config = YAML.load_file(filename)
  parse(config)
end

#read_config(yaml) ⇒ Object



29
30
31
32
# File 'lib/yes_ship_it/engine.rb', line 29

def read_config(yaml)
  config = YAML.load(yaml)
  parse(config)
end

#release_archive_file_nameObject



104
105
106
# File 'lib/yes_ship_it/engine.rb', line 104

def release_archive_file_name
  File.basename(release_archive)
end

#release_urlObject



88
89
90
# File 'lib/yes_ship_it/engine.rb', line 88

def release_url
  "https://github.com/#{github_project_name}/releases/tag/#{tag}"
end

#runObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/yes_ship_it/engine.rb', line 119

def run
  failed_assertions = []
  errored_assertions = []
  skipped_assertions = []

  @assertions.each do |assertion|
    out.print "Checking #{assertion.display_name}: "
    if dependency_errored?(assertion, errored_assertions) ||
       dependency_errored?(assertion, skipped_assertions)
      out.puts "skip (because dependency errored)"
      skipped_assertions << assertion
      next
    end
    begin
      success = assertion.check
      if success
        out.puts success
      else
        out.puts "fail"
        failed_assertions.push(assertion)
      end
    rescue AssertionError => e
      out.puts "error"
      out.puts "  " + e.message
      errored_assertions.push(assertion)
    end
  end

  out.puts

  if !errored_assertions.empty?
    out.puts "Couldn't ship #{project_name}. Help me."
    return 1
  else
    if failed_assertions.empty?
      if tag_date
        out.puts "#{project_name} #{version} already shipped on #{tag_date}"
      else
        out.puts "#{project_name} #{version} already shipped"
      end
      return 0
    else
      failed_assertions.each do |assertion|
        if dry_run?
          out.print "Dry run: "
        end
        out.print "Asserting #{assertion.display_name}: "
        begin
          success = assertion.assert(executor)
        rescue AssertionError => e
          out.puts "error"
          out.puts "  " + e.message
          out.puts
          out.puts "Ran into an error. Stopping shipping."
          return 1
        end
        out.puts success
      end

      out.puts
      if dry_run?
        out.puts "Did a dry run of shipping #{project_name} #{version}." +
          " Nothing was changed."
      else
        out.puts "Shipped #{project_name} #{version}. Hooray!"
      end
      return 0
    end
  end
end

#tagObject



96
97
98
# File 'lib/yes_ship_it/engine.rb', line 96

def tag
  "v#{version}"
end