Giter Club home page Giter Club logo

crun's Introduction

Travis-CI Build Status CircleCI Build Status Release

crun

Crystal Run : shebang wrapper for Crystal

crun is a tool enabling one to put a "bang line" in the source code of a Crystal program to run it, or to run such a source code file explicitly. It was inspired by gorun and created in an attempt to make experimenting with Crystal more appealing to people used to Ruby and similar languages which operate most visibly with source code.

Example

As an example, copy the following content to a file named "hello.cr" (or "hello", if you prefer):

#!/usr/bin/env crun

puts "Hello world"

Then, simply run it:

$ chmod +x hello.cr
$ ./hello.cr
Hello world!

Features

crun will:

  • write files under a safe directory in $CRUN_CACHE_PATH, $XDG_CACHE_HOME/crun, ~/.cache/crun, ~/.cache/.crun or .crun in this order, so that the actual script location isn't touched (may be read-only)
  • avoid races between parallel compilation of the same file
  • automatically clean up old compiled files that remain unused for some time, by default each 7 days but can be overriden by setting CLEAN_CACHE_DAYS
  • replace the process rather than using a child
  • pass arguments to the compiled application properly
  • handle well shards with comment containing dependencies of a classical shards.yml file. Anchors used can be changed by settings CRUN_SHARDS_START_ANCHOR (default: ---) and CRUN_SHARD_END_ANCHOR (default: ...).

Shards support example

#!/usr/bin/env crun
# ---
# minitest:
#   github: ysbaddaden/minitest.cr
# ...

class Foo
  def bar
    "baz"
  end
end

require "minitest/autorun"

class FooTest < Minitest::Test
  def foo
    @foo ||= Foo.new
  end

  def test_that_foo_bar_baz
    assert_equal "baz", foo.bar
  end
end

describe Foo do
  let(:foo) { Foo.new }

  describe "when asked about bar" do
    it "must respond baz" do
      foo.bar.must_equal("baz")
    end
  end
end

Where are the compiled files kept?

They are kept under $CRUN_CACHE_PATH, $XDG_CACHE_HOME/crun, ~/.cache/crun, ~/.cache/.crun or .crun in this order, in a directory named after the hostname and the slug of the source file name.

You can remove these files, but there's no reason to do this. These compiled files will be garbage collected by crun itself after a while once they stop being used. This is done in a fast and safe way so that concurrently executing scripts will not fail to execute.

How to build and install crun from source

make release
make install

You can change PREFIX or BINDIR environment variable, see Makefile

Usage

usage: crun <source file> [...]

Add Linux binfmt support

echo ':crystal:E::cr::/usr/local/bin/crun:OC' \
  | sudo tee /proc/sys/fs/binfmt_misc/register

or

make binfmt

Development

Install Git pre-commit hook

make githook

Makefile help

> make
targets:
  auto            Run tests suite continuously on writes
  binfmt          Add Linux binfmt support
  check           Run Ameba static code check
  clean           Remove crun builded binary
  clobber         Clean and remove editor backup files (*~)
  crun            Build crun binary
  format          Run Crystal format tool
  githook         Install Git pre-commit hook
  help            Show this help
  install         Install crun binary
  release         Build crun binary
  spec            Run crun specs
  tests           Run tests suite
  todo            Show fixme and todo comments
  uninstall       Uninstall crun binary

OsX (for fancy autotests / continuous testing)

brew tap veelenga/tap
brew install ameba crystal fswatch imagemagick terminal-notifier

or

make osx

Debian/Ubuntu (for fancy autotests / continuous testing)

apt install -y -q inotify-tools libnotify-bin

Contributing

  1. Fork it (https://github.com/Val/crun/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • Val Laurent Vallar - creator, maintainer
  • bew Benoit de Chezelles

crun's People

Contributors

bew avatar val avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

seanpm2001

crun's Issues

Error deleting file *.lock

Description

I want to process some text stream with my program (the same program) as in :

echo "hello\nworld\n" | ./myprog.cr  process-lines | ./myprog.cr  process-columns

When using the crystal compiler like in #! usr/bin/env crystal I got an error so following a issue
I found crun. From the description I think it should handle my case but when using it
I found the following error :

Unhandled exception: Error deleting file '/home/ nieto/.cache/crun/writter.crystal.lock': No such file or directory (Errno)
Failed to raise an exception: END_OF_STACK
[0x55c92d246da6] ???
[0x55c92d201afb] __crystal_raise +43
[0x55c92d202af5] ???
[0x55c92d20b131] ???
[0x55c92d2067ac] ???
[0x55c92d205bff] ???
[0x55c92d21f46d] ???
[0x55c92d21d9a5] ???
[0x55c92d20429a] main +74
[0x7f8f791e309b] __libc_start_main +235
[0x55c92d20087a] _start +42
[0x0] ???
minimal program
#!/usr/bin/env crun

while line = gets 
  puts line
end 

or

#!/usr/bin/env crun 
sleep 1

Support to build from STDIN

Example – Run crun with Kakoune:

evaluate-commands %sh{
  crun <<-'EOF'
    # ---
    # kakoune:
    #   github: alexherbo2/kakoune.cr
    # ---
    require "kakoune"

    ...
EOF
}

perf when running an already built script

I think it should be safe to not call shards install if there's already a lockfile. This saves a lot of time, especially if the network is slow.

diff --git a/src/build.cr b/src/build.cr
index 146acd3..448650b 100644
--- a/src/build.cr
+++ b/src/build.cr
@@ -20,7 +20,8 @@ module Crun
     hash = Dir.cd(build_dir) do
       if shards_yaml
         build_shards_config
-        hash = build_subprocess("shards", %w[install])
+        hash =
+          build_subprocess("shards", %w[install]) unless File.exists? "#{build_dir}/shards.lock"
       end
 
       hash || build_subprocess("crystal", ["build", "-o", build_path, SOURCE])

Linux binfmt support fails because of cached compiled file ending with a .cr extension

Thanks for this project.

In current master build_path is set to

@@build_path ||= "#{build_dir}/#{SOURCE_FILENAME}"

This means that build_path is likely to end with a .cr extension. In case binfmt support for .cr file has been enabled, crun will be called again when the cached compiled file is run

Process.exec(build_path, ARGS)

Adding an extra extension to ensure that cached compiled file does not end with a .cr extension should fix the problem

@@build_path ||= "#{build_dir}/#{SOURCE_FILENAME}.crystal"

Support to build from string

Example – Simple usage:

crun eval 'puts ARGV' -- 1 2 3

Example – Run crun with Kakoune:

$ crun eval %{
  # ---
  # kakoune:
  #   github: alexherbo2/kakoune.cr
  # ---
  require "kakoune"

  ...
} -- %val{session} %val{client}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.