forked from berkshelf/berkshelf-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thorfile
100 lines (85 loc) · 2.6 KB
/
Thorfile
1
2
3
4
5
6
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
# encoding: utf-8
$:.push File.expand_path("../lib", __FILE__)
$:.push File.expand_path("../tasks", __FILE__)
require 'bundler'
require 'bundler/setup'
require 'thor'
require 'berkshelf-api'
require 'octokit'
require 'berkflow/thor_tasks'
require 'build_gem'
class Default < Thor
include Thor::Actions
namespace "build"
default_task :all
GITHUB_ORG_REGEX = /(git\@github.com\:|https\:\/\/github.com\/)(.+).git/.freeze
PKG_DIR = File.expand_path("../pkg", __FILE__).freeze
VENDOR_DIR = File.expand_path("../vendor", __FILE__).freeze
PROJECT_DIR = File.dirname(__FILE__)
desc "all", "clean, package, and release to Github"
def all
clean
package
release
end
desc "clean", "clean the packaging directory"
def clean
say "cleaning..."
[ PKG_DIR, VENDOR_DIR ].each do |dir|
FileUtils.rm_rf(dir)
FileUtils.mkdir_p(dir)
end
end
desc "package", "package the software for release"
def package
say "packaging..."
empty_directory PKG_DIR
invoke "gem:build"
inside(File.dirname(__FILE__)) do
run "bundle package --all"
files = `git ls-files | grep -v spec/`.split("\n")
run("tar -czf #{archive_out} #{files.join(' ')} vendor")
end
end
method_option :github_token,
type: :string,
default: ENV["GITHUB_TOKEN"],
required: true,
aliases: "-t",
banner: "TOKEN"
desc "release", "release the packaged software to Github"
def release
say "releasing..."
begin
invoke "gem:release"
rescue => ex
raise ex unless ex.message.scan(/Repushing of gem versions is not allowed/).any?
end
begin
release = github_client.create_release(repository, version)
rescue Octokit::UnprocessableEntity
release = github_client.releases(repository).find { |release| release[:tag_name] == version }
end
say "Uploading #{File.basename(archive_out)} to Github..."
github_client.upload_asset(release[:url], archive_out, name: "berkshelf-api.tar.gz",
content_type: "application/x-tar")
invoke Berkflow::ThorTasks, "release", [], berksfile: File.join(PROJECT_DIR, "cookbook", "Berksfile")
end
private
def archive_out
File.join(PKG_DIR, "berkshelf-api-#{version}.tar.gz")
end
def github_client
@github_client ||= Octokit::Client.new(access_token: options[:github_token])
end
def repository
@repository ||= extract_repository
end
def extract_repository
_, repository = `git remote show origin | grep Push`.scan(GITHUB_ORG_REGEX).first
repository
end
def version
"v#{Berkshelf::API::VERSION}"
end
end