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
cli: add "amber exec" command for executing one-liners (#352) #371
Conversation
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.
You also probably need to load the models.
@elorest interestingly it seems I don't, at least in the app I'm testing e.g.:
|
I was planning on building a concept similar to this that opened up an editor and let you type whatever code you wanted. On exit it would run and print the output. Similar in a way to edit in pry but with the REPL. |
@elorest I really like that idea, so maybe we should just wait for you to do that |
This command could probably do both of those. The specs in Amber are just using |
@elorest kind of the way |
Yeah pretty much.
…On Nov 8, 2017 10:20 PM, "Sam Johnson" ***@***.***> wrote:
@elorest <https://github.com/elorest> kind of the way git commit works?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#371 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAHmpOSHO9i8PJe_vQ4bwkB5LbX7evdnks5s0ouGgaJpZM4QXcbu>
.
|
I'd default it to vim but let them pass any editor command in as an option.
…On Nov 8, 2017 10:28 PM, ***@***.*** wrote:
@elorest <https://github.com/elorest> kind of the way git commit works?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#371 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAHmpOSHO9i8PJe_vQ4bwkB5LbX7evdnks5s0ouGgaJpZM4QXcbu>
.
|
@elorest I'd agree with that but I think it also might be nice maybe as a separate PR to make it so that you can specify an AMBER_EDITOR environment variable so |
@elorest @sam0x17 i was trying to build a similar concept. You should give it a look https://github.com/veelenga/vicr. I often use it to answer questions on SO :) Brew installation is broken, but you can just run it locally with |
b7e9e02
to
18a8bda
Compare
Yeah you could also look at an ENV var but if not ENV or flag is provided I would default to vim.
|
@veelenga What I'm thinking of is really simple, maybe like 5 additional lines of code. I'll look at that code you linked though. |
Just throwing this to see what you guys think Workflow: I basically did a proof of concept, kinda within an amber project dir If you happen to type With this exec who needs a console! Nice work @sam0x17 @veelenga I really like what you did! |
@eliasjpr thanks -- they way I dealt with that particular issue if you look at my force-push from last night is I delete the file right after it is compiled, so no need to add it to .gitignore since it only exists on the filesystem for a few milliseconds. You will notice I also added support for doing a one-liner inline without opening the editor, and I added support for specifying a path to an existing .cr file. I'm doing some specs for this tonight so after that should be ready to review. Note that this .cr file detection is performed by checking if the argument to |
side note: I would be curious to know what trick ICR is using to pretty print models from granite-orm. e.g. in ICR when I print a model, I get all the attributes, but with this I just get <ModelName:memory_address> |
@eliasjpr do you think it would be useful to include a |
Use model.inspect to get all the information.
My suggestion would be to add temp files to |
@elorest I think we can use https://crystal-lang.org/api/0.23.1/Tempfile.html here |
@faustinoaq @elorest ok I'll change it to use Tempfile -- btw just pushed changes that add the |
@sam0x17 I don't think that a persist option is the best route. Just store all files with a timestamp in |
@elorest that makes sense -- feel free to remove the --persist option in your commit when you add the timestamp stuff |
thoughts about enclosing the code for one-liners in a puts (code).inspect e.g. I just went to use |
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.
Looks good overall - just have the spec file wrapped in the check for the build flag, and add that context
wrapper.
spec/amber/cli/commands/exec_spec.cr
Outdated
describe "amber exec" do | ||
cleanup | ||
scaffold_app(TESTING_APP) | ||
`shards` |
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 this test requires a build, it should be wrapped checking for the run_build_tests
flag.
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.
Like this: https://github.com/amberframework/amber/pull/354/files#diff-5fff94200507b682866348a325e3bd88R1
{% if flag?(:run_build_tests) %}
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.
Done!
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.
Sorry, I forgot about one of the implications of this, you have to change a line in the Dockerfile to make these tests run on Travis. Right now, the test suite is a false green because the tests for this PR are skipped.
https://github.com/sam0x17/amber/blob/amber-exec/Dockerfile#L9
Needs to be CMD ["crystal", "spec", "-D", "run_build_tests"]
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.
It has been a pending change from here: https://github.com/amberframework/amber/pull/354/files#diff-654a8a2c7db020a0d9cf8c58982c3864R9
That's the big Spec PR that has been held up for various reasons.
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.
You're breaking my heart man.
spec/amber/cli/commands/exec_spec.cr
Outdated
# This one weird trick, to make the specs pass in linux leaves scientists puzzled. | ||
Dir.glob("tmp/*") | ||
logs = Dir.glob("./tmp/*_console_result.log") | ||
File.read(logs.last?.to_s).should eq expected_result |
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.
We get more helpful failures, rather than errors with a nil check:
logs.last?.should_not be nil
(File.read(logs.last?.to_s).should eq expected_result) if logs.last?
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.
This change is optional.
spec/amber/cli/commands/exec_spec.cr
Outdated
Dir.glob("tmp/*") | ||
logs = Dir.glob("./tmp/*_console_result.log") | ||
File.read(logs.last?.to_s).should eq "1000\n" | ||
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.
Wrap the two above with context "with edit flag '-e'" do ... end
spec/amber/cli/commands/exec_spec.cr
Outdated
MainCommand.run(["exec", "-e", "tail", "-b", "1"]) | ||
logs = `ls tmp/*_console_result.log`.strip.split(/\s/) | ||
File.read(logs.last?.to_s).should eq "1000\n" | ||
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.
Wrap lines 28-38 in a context "in editor mode" do ... end
* cli: add "amber exec" command for executing one-liners (#352) * this now behaves as close to a console as we can make it * finishing touches for amber exec * do not run editor unless in editor mode * always return a result * minor wording change in error message * amber exec specs * cleaned up code a bit and copied existing file to new temp file for edits * specs pass yay. MainCommand.run always returns nil which makes testing hard * new push to see if travis passes. Pass locally so I'm confused. * debugging to figure out why travis doesn't pass * added sleeps to see if that works * one last thing * what the what? * i hope this will clear it up * weird hack that i think fixes specs on linux * commented weird hack * native ls had better work * formatted and fixed tests probably * removed some vestigual code. * removed macro flag for now * test47 of travis * namespaces specs * sorted ls results
* cli: add "amber exec" command for executing one-liners (#352) * this now behaves as close to a console as we can make it * finishing touches for amber exec * do not run editor unless in editor mode * always return a result * minor wording change in error message * amber exec specs * cleaned up code a bit and copied existing file to new temp file for edits * specs pass yay. MainCommand.run always returns nil which makes testing hard * new push to see if travis passes. Pass locally so I'm confused. * debugging to figure out why travis doesn't pass * added sleeps to see if that works * one last thing * what the what? * i hope this will clear it up * weird hack that i think fixes specs on linux * commented weird hack * native ls had better work * formatted and fixed tests probably * removed some vestigual code. * removed macro flag for now * test47 of travis * namespaces specs * sorted ls results Former-commit-id: 3a55d2a
Description of the Change
Adds a simple CLI command for executing one-liner pieces of crystal code within the application scope and printing the result.
Syntax:
update:
amber encrypt
-style syntax using your preferred text editorrelated issue #352
Alternate Designs
amber play
: bad because it requires a web browser and suffers from similar re-running issuesBenefits
AMBER_ENV=production amber exec 'code here'
Possible Drawbacks
amber exec
only need to re-compile changed files, but this might be overkillTesting
I have manually tested this on my system. I'd be happy to write unit tests -- not familiar with specs in amber but I can dive in and figure it out if required. If someone could provide some high level advice on writing specs for this I'd greatly appreciate it (i.e. testing this command requires a dummy project skeleton to be in place).