-
-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a feature shards build
#136
Changes from 6 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require "./command" | ||
|
||
module Shards | ||
module Commands | ||
class Build < Command | ||
|
||
@@args = [] of String | ||
|
||
# command line arguments | ||
@targets = [] of String | ||
@options = [] of String | ||
|
||
def run | ||
# Install dependencies before the build | ||
Install.run(@path) | ||
# Parse to find targets and options | ||
parse_args | ||
# mkdir bin | ||
mkdir_bin | ||
if @targets.size == 0 | ||
raise Error.new("Error: No target found in shard.yml") if manager.spec.targets.nil? | ||
manager.spec.targets.each do |target| | ||
build target | ||
end | ||
else | ||
@targets.each do |name| | ||
target = manager.spec.targets.find{ |t| t.name == name } | ||
raise Error.new("Error: target \'#{name}\' is not found") if target.nil? | ||
build target | ||
end | ||
end | ||
end | ||
|
||
def parse_args | ||
is_option? = false | ||
@@args.each do |arg| | ||
is_option? = true if arg.starts_with?("-") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
if is_option? | ||
@options.push(arg) | ||
else | ||
@targets.push(arg) | ||
end | ||
end | ||
end | ||
|
||
def mkdir_bin | ||
bin_path = File.join(@path, "bin") | ||
Dir.mkdir bin_path unless Dir.exists?(bin_path) | ||
end | ||
|
||
def build(target) | ||
Shards.logger.info "Building: #{target.name}" | ||
|
||
args = ["build", target.main] + @options | ||
unless @options.includes?("-o") | ||
args.push("-o") | ||
args.push(File.join("bin", target.name)) | ||
end | ||
|
||
error = MemoryIO.new | ||
status = Process.run("crystal", | ||
args: args, | ||
output: nil, error: error) | ||
raise Error.new("#{error.to_s}") unless status.success? | ||
end | ||
|
||
def self.set_args(args : Array(String)) | ||
@@args = args | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ require "../spec" | |
module Shards | ||
abstract class Command | ||
getter path : String | ||
getter sub : String | Nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused? |
||
getter spec_path : String | ||
getter lockfile_path : String | ||
|
||
|
@@ -24,8 +25,8 @@ module Shards | |
|
||
abstract def run | ||
|
||
def self.run(path) | ||
new(path).run | ||
def self.run(path, sub = nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused change? It should be rolledback probably. |
||
new(path, sub).run | ||
end | ||
|
||
def spec | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Shards | ||
class Target | ||
getter name : String | ||
getter main : String | ||
|
||
def initialize(@name, @main) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require "../integration_helper" | ||
|
||
class BuildCommandTest < Minitest::Test | ||
|
||
def test_succeeds_for_specified_target | ||
metadata = { | ||
targets: { mock: { main: "mock.cr" } } | ||
} | ||
|
||
with_shard(metadata) do | ||
Dir.cd(application_path) do | ||
File.open "mock.cr", "w" | ||
run "shards build mock" | ||
assert File.exists?(File.join(application_path, "bin", "mock")) | ||
end | ||
end | ||
end | ||
|
||
def test_succeeds_for_all_targets | ||
metadata = { | ||
targets: { mock1: { main: "mock1.cr" }, | ||
mock2: { main: "mock2.cr" } } | ||
} | ||
|
||
with_shard(metadata) do | ||
Dir.cd(application_path) do | ||
File.open "mock1.cr", "w" | ||
File.open "mock2.cr", "w" | ||
run "shards build" | ||
assert File.exists?(File.join(application_path, "bin", "mock1")) | ||
assert File.exists?(File.join(application_path, "bin", "mock2")) | ||
end | ||
end | ||
end | ||
|
||
def test_succeeds_for_multiple_targets | ||
metadata = { | ||
targets: { mock1: { main: "mock1.cr" }, | ||
mock2: { main: "mock2.cr" }, | ||
mock3: { main: "mock3.cr" } } | ||
} | ||
|
||
with_shard(metadata) do | ||
Dir.cd(application_path) do | ||
File.open "mock1.cr", "w" | ||
File.open "mock2.cr", "w" | ||
File.open "mock3.cr", "w" | ||
run "shards build mock1 mock2" | ||
assert File.exists?(File.join(application_path, "bin", "mock1")) | ||
assert File.exists?(File.join(application_path, "bin", "mock2")) | ||
assert !File.exists?(File.join(application_path, "bin", "mock3")) | ||
end | ||
end | ||
end | ||
|
||
def test_fails_when_specified_target_is_not_found | ||
metadata = { | ||
targets: { mock: { main: "mock.cr" } } | ||
} | ||
|
||
with_shard(metadata) do | ||
Dir.cd(application_path) do | ||
File.open "mock.cr", "w" | ||
ex = assert_raises(FailedCommand) { run "shards --no-color build mock_fake" } | ||
assert_match "E: Error: target 'mock_fake' is not found\n", ex.stdout | ||
assert_empty ex.stderr | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if @targets.empty?
:)