Skip to content

Commit

Permalink
Add a thrift_version compiler option.
Browse files Browse the repository at this point in the history
If you require a specific version of the Thrift compiler, you can specify a
version requirement using the `thrift_version` option. Version requirements
use the SemVer 2.0 schema.
  • Loading branch information
jparise committed Feb 2, 2016
1 parent 1240703 commit 8ececa7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Next, define the list of `:thrift_files` that should be compiled. In this
example, we gather all of the `.thrift` files under the `thrift` directory:

```elixir
thrift_files: Mix.Utils.extract_files(["thrift"], [:thrift]),
thrift_files: Mix.Utils.extract_files(["thrift"], [:thrift])
```

By default, the generated source files will be written to the `src` directory,
Expand All @@ -46,6 +46,14 @@ but you can change that using the `thrift_output` option.
You can also pass additional options to the Thrift compiler by listing them in
the `thrift_options` option.

If you require a specific version of the Thrift compiler, you can specify a
version requirement using the `thrift_version` option. Version requirements
use the [SemVer 2.0 schema][semver]. For example:

```elixir
thrift_version: ">= 0.9.3" # Erlang maps support
```

## Thrift IDL Parsing

This package also contains experimental support for parsing [Thrift IDL][idl]
Expand All @@ -60,4 +68,5 @@ provide more advanced parser support over time.
{:ident, 1, 'BLUE'}, {:symbol, 1, '}'}], 1}
```

[semver]: http://semver.org/
[idl]: https://thrift.apache.org/docs/idl
16 changes: 16 additions & 0 deletions lib/compile.thrift.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ defmodule Mix.Tasks.Compile.Thrift do
* `:thrift_options` - list of additional options that will be passed to
the Thrift compiler.
* `:thrift_version` - thrift compiler `Version` requirement
"""

@spec run(OptionParser.argv) :: :ok | :noop
Expand All @@ -32,17 +34,31 @@ defmodule Mix.Tasks.Compile.Thrift do
project = Mix.Project.config
thrift_files = project[:thrift_files]
thrift_options = project[:thrift_options] || []
thrift_version = project[:thrift_version]
output_dir = project[:thrift_output] || "src"

stale_files = Enum.filter(thrift_files, fn file ->
force || stale?(file, output_dir)
end)

if(thrift_version && !Enum.empty?(stale_files)) do
unless(Version.match?(v = get_thrift_version, thrift_version)) do
Mix.raise "Unsupported Thrift version #{v} (requires #{thrift_version})"
end
end

unless(Enum.empty?(stale_files), do: File.mkdir_p!(output_dir))

Enum.each stale_files, &generate(&1, output_dir, thrift_options)
end

defp get_thrift_version do
case System.cmd("thrift", ~w[-version]) do
{s, 0} -> hd(Regex.run(~r/\b(\d+\.\d+\.\d+)\b/, s, capture: :first) || [])
{_, e} -> Mix.raise "Failed to execute `thrift -version` (error #{e})"
end
end

defp get_generated_files(thrift_file, output_dir) do
basename = Path.basename(thrift_file, ".thrift")
pattern = basename <> "_{constants,thrift,types}.{erl,hrl}"
Expand Down

0 comments on commit 8ececa7

Please sign in to comment.