Skip to content

Add run command #298

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

Closed
wants to merge 12 commits into from
Closed

Add run command #298

wants to merge 12 commits into from

Conversation

denolfe
Copy link
Contributor

@denolfe denolfe commented Oct 8, 2019

Adds shards run command. Resolves #157.

@denolfe denolfe mentioned this pull request Oct 8, 2019
@ysbaddaden
Copy link
Contributor

Thanks. Can you write a few integration tests, to check that the targets are built and run, and the exit status properly reported?

@denolfe
Copy link
Contributor Author

denolfe commented Oct 9, 2019

For sure. Is there already a way to evaluate the exit status somewhere in the tests? This is my first go with minitest, so it's a little difficult to debug my tests. I see that run from factories.cr is used in the build_test.cr tests.

@denolfe
Copy link
Contributor Author

denolfe commented Oct 10, 2019

Tests added, but 2 of them are behaving unexpectedly. I've verified the functionality in a dummy repo and using the bin/shards that is compiled. I'm not too sure why these are behaving differently within the tests.

The 2 tests in question are expected to throw a FailedCommand but are not.

Any help is welcome.

@ysbaddaden
Copy link
Contributor

The YAML blocks create a YAML with 2 leading spaces, it should be the following (notice that the blocking YAML is indented):

str = <<-YAML
  name: xxx
  version: 0.0.0
  YAML

@denolfe
Copy link
Contributor Author

denolfe commented Oct 13, 2019

Hmm, there appears to be an issue beyond just that. Modifying as suggested did not make a difference.

@ysbaddaden
Copy link
Contributor

Yes, youre not changing the current directory in some tests.

Copy link
Contributor

@ysbaddaden ysbaddaden left a comment

Choose a reason for hiding this comment

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

Just a few error message changes. All raised Error exceptions follow this schema a capitalized Term to be colorized followed by a lowercase sentence. For example Error missing ... or Failed to ....

Also, I think the run command should just pass STDIN, STDOUT and STDERR as-is. Maybe even replacing the current process with exec — but I'm not sure it's possible with Crystal.

@denolfe denolfe marked this pull request as ready for review October 15, 2019 15:39
@denolfe denolfe requested a review from ysbaddaden October 15, 2019 15:40
@denolfe
Copy link
Contributor Author

denolfe commented Oct 18, 2019

Also, I think the run command should just pass STDIN, STDOUT and STDERR as-is. Maybe even replacing the current process with exec — but I'm not sure it's possible with Crystal.

@ysbaddaden Was this something that still needs to be addressed or just a nice to have?

@RX14
Copy link
Member

RX14 commented Oct 29, 2019

It should really use Process.exec, since we have it bound easily in the stdlib.

Co-Authored-By: Johannes Müller <johannes.mueller@smj-fulda.org>
Copy link
Member

@RX14 RX14 left a comment

Choose a reason for hiding this comment

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

One nitpick, but it looks great to me.

@denolfe
Copy link
Contributor Author

denolfe commented Dec 5, 2019

@ysbaddaden Anything else needed on this one before getting approved?

@Sija
Copy link
Contributor

Sija commented Mar 24, 2020

@denolfe Specs needs to be rewritten (see #334).

Co-Authored-By: Sijawusz Pur Rahnama <sija@sija.pl>
@bcardiff
Copy link
Member

Can we have a spec checking how to pass ARGV values from the shards run command to the actual command? I guess it should be shards run foo -- bar, but I am not sure from the code if that's the idea.

@denolfe
Copy link
Contributor Author

denolfe commented Mar 25, 2020

I've confirmed manually that passing ARGV values works, but simulating it using the run function from factories.cr has proven to be difficult. It spits errors out to the console when the test is run saying it cannot read ARGV[0] which is being passed. Here is the WIP test code:

  it "runs specific target with args" do
    File.write(File.join(application_path, "src", "cli_with_args.cr"), "puts __FILE__;args = ARGV[0]")

    File.write File.join(application_path, "shard.yml"), <<-YAML
      name: build
      version: 0.1.0
      targets:
        app:
          main: src/cli_with_args.cr
      YAML

    Dir.cd(application_path) do
      run "shards run app --no-color -- foo"
      File.exists?(bin_path("app")).should be_true
      `#{bin_path("app")}`.chomp.should eq(File.join(application_path, "src", "cli_with_args.cr"))
    end
  end

Could this be something specific with how ARGV is handled in a shell environment vs. through Process.exec? Any help appreciated.

@silasb
Copy link

silasb commented Jul 21, 2020

Anything I can do to help move this feature over the finish line?

@denolfe
Copy link
Contributor Author

denolfe commented Jul 23, 2020

Can we have a spec checking how to pass ARGV values from the shards run command to the actual command? I guess it should be shards run foo -- bar, but I am not sure from the code if that's the idea.

@silasb The only piece missing was a test for the above. I'm able to verify functionality manually, but testing it properly proved to be difficult because of how the commands are fed to the shell.

@denolfe
Copy link
Contributor Author

denolfe commented Jan 4, 2021

I no longer use Crystal personally and don't have time to get these tests running, so I'll be closing this PR out. My branch will still be accessible if someone wanted to use it to finish #157.

@denolfe denolfe closed this Jan 4, 2021
luislavena added a commit to luislavena/shards that referenced this pull request Mar 5, 2022
Introduces `shards run`, similar to `shards build`, that builds and runs
the specified target, providing a unified interface to developers.

With this, a developer can directly use `shards run <target>` in a
single step without having first to compile (build) and then execute it
with `./bin/target`. No need for a script or wrapper.

This is inspired by `crystal build` and `crystal run` user experience
(with the difference that `crystal run` creates a temporary binary).

This is very helpful for automatic rebuild scenarios, where a process is
monitoring your code and can rebuild and relaunch your application.

When invoking `shards run`, it will first build the target (with the
supplied options) and then invoke it, passing the extra options:

  $ shards run myapp --stats -- --args

Is the same as:

  $ shards build myapp --stats

  $ ./bin/myapp --args

If the target exits with error, the exit code is passed back.

Resolves crystal-lang#157
Relates crystal-lang#298

Co-authored-by: Elliot DeNolf <65888+denolfe@users.noreply.github.com>
@luislavena luislavena mentioned this pull request Mar 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Run command
7 participants