-
Notifications
You must be signed in to change notification settings - Fork 13
/
fig
executable file
·190 lines (169 loc) · 5.28 KB
/
fig
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
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
#!/usr/bin/env ruby
$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
require 'rubygems'
require 'net/ftp'
require 'fig/retriever'
require 'fig/options'
require 'fig/environment'
require 'fig/repository'
require 'fig/os'
require 'fig/parser'
require 'fig/windows'
include Fig
def parse_descriptor(descriptor)
# todo should use treetop for these:
package_name = descriptor =~ /^([^:\/]+)/ ? $1 : nil
config_name = descriptor =~ /:([^:\/]+)/ ? $1 : nil
version_name = descriptor =~ /\/([^:\/]+)/ ? $1 : nil
return package_name, config_name, version_name
end
shell_command = nil
ARGV.each_with_index do |arg, i|
if arg == "-"
# $stderr.puts "Use of single dash (-) is deprecated. Use double dash (--) instead"
# exit 1
elsif arg == "--"
shell_command = ARGV[(i+1)..-1]
ARGV.slice!(i..-1)
break
end
end
options, argv = parse_options(ARGV)
vars = {}
ENV.each {|key,value| vars[key]=value }
remote_url = nil
if options[:update] || options[:publish] || options[:update_if_missing] || options[:list_remote]
remote_url = ENV['FIG_REMOTE_URL']
if remote_url.nil?
$stderr.puts "Please define the FIG_REMOTE_URL environment variable"
exit 1
end
end
remote_user = nil
if options[:publish]
# remote_user = ENV['FIG_REMOTE_USER']
# if remote_user.nil?
# $stderr.puts "Please define the FIG_REMOTE_USER environment variable"
# exit 1
# end
end
os = OS.new(options[:login])
repos = Repository.new(os, File.expand_path(File.join(options[:home], 'repos')), remote_url, remote_user, options[:update], options[:update_if_missing])
retriever = Retriever.new(".")
at_exit { retriever.save }
env = Environment.new(os, repos, vars, retriever)
options[:modifiers].each do |modifier|
env.apply_config_statement(nil, modifier, nil)
end
#if File.exist?(".fig")
# $stderr.puts "The '.fig' file is deprecated. Please rename to 'package.fig'"
# exit 1
#end
DEFAULT_FIG_FILE = 'package.fig'
input = nil
if options[:input] == :none
# ignore
elsif options[:input] == '-'
input = $stdin.read
elsif options[:input].nil?
input = os.read(DEFAULT_FIG_FILE) if os.exist?(DEFAULT_FIG_FILE)
else
if os.exist?(options[:input])
input = os.read(options[:input])
else
$stderr.puts "File not found: #{options[:input]}"
exit 1
end
end
options[:cleans].each do |descriptor|
package_name, version_name = descriptor.split('/')
repos.clean(package_name, version_name)
end
if options[:list]
repos.list_packages.sort.each do |item|
puts item
end
exit 0
end
if options[:list_remote]
repos.list_remote_packages.sort.each do |item|
puts item
end
exit 0
end
if not options[:list_configs].empty?
options[:list_configs].each do |descriptor|
package_name, version_name = descriptor.split('/')
repos.read_local_package(package_name, version_name).configs.each do |config|
puts config.name
end
end
exit 0
end
if input
package = Parser.new.parse_package(nil, nil, ".", input)
direct_retrieves=[]
if options[:retrieve]
package.retrieves.each do |var, path|
if var =~ /^@([^\/]+)(.*)/
direct_retrieves << [$1, $2, path]
else
env.add_retrieve(var, path)
end
end
end
unless options[:publish] || options[:list] || options[:publish_local]
env.register_package(package)
env.apply_config(package, options[:config], nil)
direct_retrieves.each do |info|
env.direct_retrieve(info[0], info[1], info[2])
end
end
else
package = Package.new(nil, nil, ".", [])
end
if options[:publish] || options[:publish_local]
if !argv.empty?
$stderr.puts "Unexpected arguments: #{argv.join(' ')}"
exit 10
end
package_name, config_name, version_name = parse_descriptor(options[:publish] || options[:publish_local])
if package_name.nil? || version_name.nil?
$stderr.puts "Please specify a package name and a version name"
exit 10
end
if not options[:modifiers].empty?
publish_statements = options[:resources] + options[:archives] + [Configuration.new("default", options[:modifiers])]
publish_statements << Publish.new("default","default")
elsif not package.statements.empty?
publish_statements = package.statements
else
$stderr.puts "Nothing to publish"
exit 1
end
if options[:publish]
puts "Checking status of #{package_name}/#{version_name}..."
if repos.list_remote_packages.include?("#{package_name}/#{version_name}")
puts "#{package_name}/#{version_name} has already been published"
if not options[:force]
puts "Use the --force option if you really want to overwrite, or us --publish-local for testing"
exit 1
else
puts "Overwriting..."
end
end
end
puts "Publishing #{package_name}/#{version_name}"
repos.publish_package(publish_statements, package_name, version_name, options[:publish_local])
elsif options[:echo]
puts env[options[:echo]]
elsif shell_command
argv.shift
env.execute_shell(shell_command) { |cmd| os.shell_exec cmd }
elsif argv[0]
package_name, config_name, version_name = parse_descriptor(argv.shift)
env.include_config(package, package_name, config_name, version_name, nil)
env.execute_config(package, package_name, config_name, nil, argv) { |cmd| os.shell_exec cmd }
elsif input
env.execute_config(package, nil, options[:config], nil, argv) { |cmd| os.shell_exec cmd }
end