Module: Capistrano::Sbt

Defined in:
lib/capistrano-sbt.rb,
lib/capistrano-sbt/deploy.rb,
lib/capistrano-sbt/version.rb

Constant Summary collapse

VERSION =
"0.0.6"

Class Method Summary collapse

Class Method Details

.extended(configuration) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/capistrano-sbt/deploy.rb', line 7

def self.extended(configuration)
  configuration.load {
    namespace(:sbt) {
      _cset(:sbt_version, '0.12.2')
      _cset(:sbt_group_id) {
        case sbt_version
        when /^0\.(?:7|10)\.\d+$/, /^0\.11\.[0-2]$/
          'org.scala-tools.sbt'
        else
          'org.scala-sbt'
        end
      }
      _cset(:sbt_jar_url) {
        "http://typesafe.artifactoryonline.com/typesafe/ivy-releases/#{sbt_group_id}/sbt-launch/#{sbt_version}/sbt-launch.jar"
      }
      _cset(:sbt_jar_file) {
        File.join(shared_path, 'tools', 'sbt', "sbt-#{sbt_version}", File.basename(URI.parse(sbt_jar_url).path))
      }
      _cset(:sbt_jar_file_local) {
        File.join(File.expand_path('.'), 'tools', 'sbt', "sbt-#{sbt_version}", File.basename(URI.parse(sbt_jar_url).path))
      }
      _cset(:sbt_use_extras, false)
      _cset(:sbt_extras_url, "https://raw.github.com/paulp/sbt-extras/master/sbt")
      _cset(:sbt_extras_file) { File.join(shared_path, 'tools', 'sbt', 'sbt') }
      _cset(:sbt_extras_file_local) { File.join(File.expand_path('.'), 'tools', 'sbt', 'sbt') }
      _cset(:sbt_extras_check_interval, 86400)
      _cset(:sbt_extras_check_timestamp) { (Time.now - sbt_extras_check_interval).strftime('%Y%m%d%H%M') }
      _cset(:sbt_cmd) {
        if fetch(:sbt_java_home, nil)
          env = "env JAVA_HOME=#{sbt_java_home.dump}"
          java = "#{sbt_java_home}/bin/java"
        else
          env = ""
          java = "java"
        end
        if sbt_use_extras
          "#{env} #{sbt_extras_file} #{sbt_options.join(' ')}".strip
        else
          "#{env} #{java} -jar #{sbt_jar_file} #{sbt_options.join(' ')}".strip
        end
      }
      _cset(:sbt_cmd_local) {
        if fetch(:sbt_java_home_local, nil)
          env = "env JAVA_HOME=#{sbt_java_home_local.dump}"
          java = "#{sbt_java_home_local}/bin/java"
        end
        if sbt_use_extras
          "#{env} #{sbt_extras_file_local} #{sbt_options_local.join(' ')}".strip
        else
          "#{env} #{java} -jar #{sbt_jar_file_local} #{sbt_options_local.join(' ')}".strip
        end
      }
      _cset(:sbt_project_path) {
        release_path
      }
      _cset(:sbt_project_path_local) {
        Dir.pwd
      }
      _cset(:sbt_target_path) {
        File.join(sbt_project_path, 'target')
      }
      _cset(:sbt_target_path_local) {
        File.join(sbt_project_path_local, File.basename(sbt_target_path))
      }
      _cset(:sbt_template_path, File.join(File.dirname(__FILE__), 'templates'))
      _cset(:sbt_update_settings, false)
      _cset(:sbt_update_settings_locally, false)
      _cset(:sbt_settings_path) { File.join(sbt_project_path, 'sbt') }
      _cset(:sbt_settings_path_local) { File.join(sbt_project_path_local, 'sbt') }
      _cset(:sbt_settings, [])
      _cset(:sbt_settings_local, [])
      _cset(:sbt_cleanup_settings, [])
      _cset(:sbt_cleanup_settings_local, [])
      _cset(:sbt_compile_locally, false) # perform precompilation on localhost
      _cset(:sbt_goals, %w(reload clean package))
      _cset(:sbt_common_options) {
        options = []
        if fetch(:sbt_log_noformat, true)
          options << if sbt_use_extras
                       "-no-colors"
                     else
                       "-Dsbt.log.noformat=true"
                     end
        end
        options
      }
      _cset(:sbt_options) {
        options = sbt_common_options + fetch(:sbt_extra_options, [])
        if sbt_update_settings
          options << if sbt_use_extras
                       "-sbt-dir #{sbt_settings_path}"
                     else
                       "-Dsbt.global.base=#{sbt_settings_path}"
                     end
        end
        options
      }
      _cset(:sbt_options_local) {
        options = sbt_common_options + fetch(:sbt_extra_options_local, [])
        if sbt_update_settings_locally
          options << if sbt_use_extras
                       "-sbt-dir #{sbt_settings_path_local}"
                     else
                       "-Dsbt.global.base=#{sbt_settings_path_local}"
                     end
        end
        options
      }

      desc("Setup sbt.")
      task(:setup, :roles => :app, :except => { :no_release => true }) {
        transaction {
          install
          update_settings if sbt_update_settings
          setup_locally if sbt_compile_locally
        }
      }
      after 'deploy:setup', 'sbt:setup'

      desc("Setup sbt locally.")
      task(:setup_locally, :except => { :no_release => true }) {
        transaction {
          install_locally
          update_settings_locally if sbt_update_settings_locally
        }
      }

      def _install(options={})
        execute = []
        if sbt_use_extras
          extras_file = options.delete(:extras_file)
          execute << "mkdir -p #{File.dirname(extras_file)}"
          x = "/tmp/sbt-extras.#{$$}"
          execute << "touch -t #{sbt_extras_check_timestamp} #{x}"
          execute << "( test #{extras_file} -nt #{x} || wget --no-verbose -O #{extras_file} #{sbt_extras_url} )"
          execute << "touch #{extras_file}"
          execute << "rm -f #{x}"
          execute << "( test -x #{extras_file} || chmod a+x #{extras_file} )"
        else
          jar_file = options.delete(:jar_file)
          execute << "mkdir -p #{File.dirname(jar_file)}"
          execute << "( test -f #{jar_file} || wget --no-verbose -O #{jar_file} #{sbt_jar_url} )"
          execute << "test -f #{jar_file}"
        end
        execute.join(' && ')
      end

      task(:install, :roles => :app, :except => { :no_release => true }) {
        run(_install(:jar_file => sbt_jar_file, :extras_file => sbt_extras_file))
      }

      task(:install_locally, :except => { :no_release => true }) {
        run_locally(_install(:jar_file => sbt_jar_file_local, :extras_file => sbt_extras_file_local))
      }

      def template(file)
        if File.file?(file)
          File.read(file)
        elsif File.file?("#{file}.erb")
          ERB.new(File.read("#{file}.erb")).result(binding)
        else
          abort("No such template: #{file} or #{file}.erb")
        end
      end

      def _update_settings(files_map, options={})
        execute = []
        dirs = files_map.map { |src, dst| File.dirname(dst) }.uniq
        execute << "mkdir -p #{dirs.join(' ')}" unless dirs.empty?
        files_map.each do |src, dst|
          execute << "( diff -u #{dst} #{src} || mv -f #{src} #{dst} )"
          cleanup = options.fetch(:cleanup, [])
          execute << "rm -f #{cleanup.join(' ')}" unless cleanup.empty?
        end
        execute.join(' && ')
      end

      task(:update_settings, :roles => :app, :except => { :no_release => true }) {
        srcs = sbt_settings.map { |f| File.join(sbt_template_path, f) }
        tmps = sbt_settings.map { |f| capture("mktemp").chomp }
        dsts = sbt_settings.map { |f| File.join(sbt_settings_path, f) }
        begin
          srcs.zip(tmps).each do |src, tmp|
            put(template(src), tmp)
          end
          run(_update_settings(tmps.zip(dsts), :cleanup => sbt_cleanup_settings)) unless tmps.empty?
        ensure
          run("rm -f #{tmps.join(' ')}") unless tmps.empty?
        end
      }

      task(:update_settings_locally, :except => { :no_release => true }) {
        srcs = sbt_settings_local.map { |f| File.join(sbt_template_path, f) }
        tmps = sbt_settings.map { |f| `mktemp`.chomp }
        dsts = sbt_settings_local.map { |f| File.join(sbt_settings_path_local, f) }
        begin
          srcs.zip(tmps).each do |src, tmp|
            File.open(tmp, 'wb') { |fp| fp.write(template(src)) }
          end
          run_locally(_update_settings(tmps.zip(dsts), :cleanup => sbt_cleanup_settings_local)) unless tmps.empty?
        ensure
          run_locally("rm -f #{tmps.join(' ')}") unless tmps.empty?
        end
      }

      desc("Update sbt build.")
      task(:update, :roles => :app, :except => { :no_release => true }) {
        transaction {
          if sbt_compile_locally
            update_locally
          else
            execute
          end
        }
      }
      after 'deploy:finalize_update', 'sbt:update'

      desc("Update sbt build locally.")
      task(:update_locally, :except => { :no_release => true }) {
        transaction {
          execute_locally
          upload_locally
        }
      }

      def _sbt(cmd, path, goals=[])
        "cd #{path.dump} && #{cmd} #{goals.map { |s| s.dump }.join(' ')}"
      end

      def _sbt_parse_version(s)
        # FIXME: is there any better way to get project version?
        lastline = s.split(/(?:\r?\n)+/)[-1]
        lastline.split[-1]
      end

      _cset(:sbt_release_build, false)
      _cset(:sbt_snapshot_pattern, /-SNAPSHOT$/i)
      _cset(:sbt_project_version) {
        _sbt_parse_version(capture(_sbt(sbt_cmd, sbt_project_path, ["show version"])))
      }
      _cset(:sbt_project_version_local) {
        _sbt_parse_version(run_locally(_sbt(sbt_cmd_local, sbt_project_path_local, ["show version"])))
      }

      def _validate_project_version(version_key)
        if sbt_release_build
          version = fetch(version_key)
          if sbt_snapshot_pattern === version
            abort("Skip to build project since \`#{version}' is a SNAPSHOT version.")
          end
        end
      end

      desc("Perform sbt build.")
      task(:execute, :roles => :app, :except => { :no_release => true }) {
        on_rollback {
          run(_sbt(sbt_cmd, sbt_project_path, %w(clean)))
        }
        _validate_project_version(:sbt_project_version)
        run(_sbt(sbt_cmd, sbt_project_path, sbt_goals))
      }

      desc("Perform sbt build locally.")
      task(:execute_locally, :roles => :app, :except => { :no_release => true }) {
        on_rollback {
          run_locally(_sbt(sbt_cmd_local, sbt_project_path_local, %w(clean)))
        }
        _validate_project_version(:sbt_project_version_local)
        cmdline = _sbt(sbt_cmd_local, sbt_project_path_local, sbt_goals)
        logger.info(cmdline)
        abort("execution failure") unless system(cmdline)
      }

      _cset(:sbt_tar, 'tar')
      _cset(:sbt_tar_local, 'tar')
      _cset(:sbt_target_archive) {
        "#{sbt_target_path}.tar.gz"
      }
      _cset(:sbt_target_archive_local) {
        "#{sbt_target_path_local}.tar.gz"
      }
      task(:upload_locally, :roles => :app, :except => { :no_release => true }) {
        on_rollback {
          run("rm -rf #{sbt_target_path} #{sbt_target_archive}")
        }
        begin
          run_locally("cd #{File.dirname(sbt_target_path_local)} && #{sbt_tar_local} chzf #{sbt_target_archive_local} #{File.basename(sbt_target_path_local)}")
          upload(sbt_target_archive_local, sbt_target_archive)
          run("cd #{File.dirname(sbt_target_path)} && #{sbt_tar} xzf #{sbt_target_archive} && rm -f #{sbt_target_archive}")
        ensure
          run_locally("rm -f #{sbt_target_archive_local}")
        end
      }
    }
  }
end