Skip to content
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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Shards
#puts " info <package>"
puts " init"
puts " install"
puts " build [targets] [options]"
puts " list"
puts " prune"
#puts " search <query>"
Expand Down Expand Up @@ -52,7 +53,10 @@ module Shards
# Commands::Search.run(args[1])
when "update"
Commands::Update.run(path)
#Commands.update(*args[1 .. -1])
#Commands.update(*args[1 .. -1])
when "build"
Commands::Build.set_args(args[1..(args.size-1)]) if args.size > 1
Commands::Build.run(path)
else
display_help_and_exit(opts)
end
Expand Down
72 changes: 72 additions & 0 deletions src/commands/build.cr
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if @targets.empty? :)

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?("-")
Copy link
Contributor

@Sija Sija Nov 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use Char here: '-'

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
5 changes: 3 additions & 2 deletions src/commands/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require "../spec"
module Shards
abstract class Command
getter path : String
getter sub : String | Nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused?

getter spec_path : String
getter lockfile_path : String

Expand All @@ -24,8 +25,8 @@ module Shards

abstract def run

def self.run(path)
new(path).run
def self.run(path, sub = nil)
Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down
14 changes: 14 additions & 0 deletions src/spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require "yaml"
require "./config"
require "./dependency"
require "./errors"
require "./target"

module Shards
class Spec
Expand Down Expand Up @@ -94,6 +95,15 @@ module Shards
read_mapping(pull) { dependency[pull.read_scalar] = pull.read_scalar }
development_dependencies << dependency
end
when "targets"
read_mapping(pull) do
target_name = pull.read_scalar
read_mapping(pull) do
pull.read_next # main:
target = Target.new(target_name, pull.read_scalar)
targets << target
end
end
when "libraries"
read_mapping(pull) do
libraries << Library.new(pull)
Expand Down Expand Up @@ -140,6 +150,10 @@ module Shards
@development_dependencies ||= [] of Dependency
end

def targets
@targets ||= [] of Target
end

def libraries
@libraries ||= [] of Library
end
Expand Down
9 changes: 9 additions & 0 deletions src/target.cr
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
70 changes: 70 additions & 0 deletions test/integration/build_test.cr
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
12 changes: 12 additions & 0 deletions test/support/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ module Shards
else
yml << value
end
elsif key.to_s == "targets"
yml << "targets:\n"
if value.responds_to?(:each)
value.each do |target, info|
yml << " " << target.to_s << ":\n"
if info.responds_to?(:each)
info.each do |main, src|
yml << " main: " << src.inspect << "\n"
end
end
end
end
end
end
end
Expand Down