Git Product home page Git Product logo

mix_script's Introduction

MixScript

A build utility that allows you to to use mix packages in an elixir script.

Example

Let us say, you have a file at ~/scripts/elixir_curl.exs

mix_dep {:httpotion, ">0.0.0"}

if args == [] do
  IO.puts "invalid args"
else
  IO.inspect HTTPotion.get(hd args)
end

You can run the following:

# compile our elixir script
mix_script compile ~/scripts/elixir_curl.exs
# run it with args
~/scripts/elixir_curl http://google.com
> %HTTPotion.Response{body: "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n<TITLE>302 Moved</TITLE></HEAD><BODY>\n<H1>302 Moved</H1>\nThe document has moved\n<A HREF=\"http://www.google.co.in/?gfe_rd=cr&amp;ei=DBAaWbWTLurx8AeKp4uwCg\">here</A>.\r\n</BODY></HTML>\r\n",
 headers: %HTTPotion.Headers{hdrs: %{"cache-control" => "private",
    "content-length" => "261", "content-type" => "text/html; charset=UTF-8",
    "date" => "Mon, 15 May 2017 20:31:08 GMT",
    "location" => "http://www.google.co.in/?gfe_rd=cr&ei=DBAaWbWTLurx8AeKp4uwCg",
    "referrer-policy" => "no-referrer"}}, status_code: 302}

Installation

mix escript.install hex mix_script

This will install the binary into your $HOME/.mix/escripts directory, so make sure that is part of your $PATH

TODO

  • Make this more efficient by using a common directory for the mix packages
  • Make it usable via a shebang #!/usr/bin/env mix_script which does compilation and execution

mix_script's People

Contributors

eiji7 avatar minhajuddin avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

mix_script's Issues

SyntaxError in mix.exs:22 for every elixir script

Environment:

OS: Funtoo Linux

=== Enabled Profiles: ===

        arch: x86-64bit
       build: current
     subarch: intel64-ivybridge
              # ...

Elixir and Erlang version manager: asdf
Elixir version: 1.5.2-otp-20
Erlang version: 20.1

Steps to reproduce:

  1. mix escript.install hex mix_script
  2. update PATH environment variable
  3. touch example.exs (or just copy example from README.md)
  4. mix_script compile example.exs

Full log (replaced tmp directory name with: ...):

17:48:36.572 [info]  parsing script @ example.exs

17:48:36.581 [info]  Found these deps: 

17:48:36.581 [info]  Found this in the script: 

17:48:36.583 [info]  creating tmp dir

17:48:36.608 [info]  created tmp dir /tmp/...

17:48:36.612 [info]  executing: "mix" with args ["new", "/tmp/...", "--module", "MixScript", "--app", "mix_script"]
* creating README.md
* creating .gitignore
* creating mix.exs
* creating config
* creating config/config.exs
* creating lib
* creating lib/mix_script.ex
* creating test
* creating test/test_helper.exs
* creating test/mix_script_test.exs

Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:

    cd /tmp/...
    mix test

Run "mix help" for more commands.


17:48:36.893 [info]  created mix project

17:48:36.893 [info]  replacing deps

17:48:36.896 [info]  setting up escript

17:48:36.896 [info]  executing: "mix" with args ["deps.get"]
** (SyntaxError) mix.exs:22: "do" is missing terminator "end". unexpected token: "]" at line 26
    (elixir) lib/code.ex:334: Code.load_file/2
    (mix) lib/mix/cli.ex:26: Mix.CLI.proceed/1
    (elixir) lib/code.ex:376: Code.require_file/2
ERROR:
{"", 1}

17:48:37.180 [info]  executing: "mix" with args ["escript.build"]
** (SyntaxError) mix.exs:22: "do" is missing terminator "end". unexpected token: "]" at line 26
    (elixir) lib/code.ex:334: Code.load_file/2
    (mix) lib/mix/cli.ex:26: Mix.CLI.proceed/1
    (elixir) lib/code.ex:376: Code.require_file/2
ERROR:
{"", 1}
** (File.CopyError) could not copy from "/tmp/.../mix_script" to "/home/username/test2": no such file or directory
    (elixir) lib/file.ex:631: File.cp!/3
    (mix_script) lib/mix_script.ex:10: MixScript.main/1
    (elixir) lib/kernel/cli.ex:90: anonymous fn/3 in Kernel.CLI.exec_fun/2

Expected result:

Project should be compiled and generate a binary.

Actual result:

Project does not compile and therefore can't generate a binary.

Found problem:

Here is output of cat /tmp/.../mix.exs command:

defmodule MixScript.Mixfile do
  use Mix.Project

  def project do
    [
      app: :mix_script,
      version: "0.1.0",
      elixir: "~> 1.5",
escript: [main_module: MixScript],
      deps: deps()
    ]
  end

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      extra_applications: [:logger]
    ]
  end

  # Run "mix help deps" to learn about dependencies.
  defp deps do
[]
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"},
    ]
  end
end

As you can see at end of &deps/0 function there is extra ] character that causes SyntaxError.

Possible solutions:

  1. cond in

    could return full List that is added to returned tuple, so you can filter every line before end in this function.

  2. Generate file from your own template with current Elixir version and updated dependencies list.

Workaround

I have created PR: #2, so anyone that needs it working can now follow few simple steps:

  1. Clone my repository:
    git clone [email protected]:Eiji7/mix_script.git
    or:
    git clone https://github.com/Eiji7/mix_script.git
  2. Enter directory: cd mix_script
  3. Build project: MIX_ENV=prod mix escript.build
  4. Optionally copy mix_script compiled binary wherever you like ๐Ÿ˜„
  5. Run path/to/compiled/mix_script path/to/example.exs

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.