-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmix.exs
268 lines (232 loc) · 7.06 KB
/
mix.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
defmodule ElixirPluginTemplate.MixProject do
use Mix.Project
@emqx_metadata_vsn "0.1.2"
def project do
[
app: :elixir_plugin_template,
version: "0.1.2",
elixir: "~> 1.13",
start_permanent: Mix.env() == :prod,
# we don't consolidate protocols to avoid clashes with other
# plugins or Elixir itself which is possibly running inside the
# EMQX node.
consolidate_protocols: false,
deps: deps(),
releases: releases()
]
end
def application() do
[
mod: {ElixirPluginTemplate.Application, []}
]
end
def releases() do
[
# NOTE: this must be named the same as the application!
elixir_plugin_template: [
applications: [
elixir_plugin_template: :permanent
],
include_erts: false,
steps: [
:assemble,
&adjust/1,
&emqx_tar/1
],
emqx_plugin_info: %{
authors: ["EMQX Team"],
builder: %{
name: "EMQX Team",
contact: "[email protected]",
website: "www.emqx.com"
},
repo: "https://github.com/emqx/emqx-elixir-plugin-template",
functionality: ["Demo"],
compatibility: %{
emqx: "~> 5.7.0"
},
description: "This is a demo plugin"
},
emqx_plugin_opts: [
include_src?: true,
## Set this to `false` if your EMQX installation does not already ship with
## Elixir.
exclude_elixir?: true,
## Set`:with_config_schema?` to `true` if your application uses its own Avro
## configuration.
## See `priv/config.hocon.example`, `config_i18n.json.example`, and
## `priv/config_schema.avsc{.enterprise,}.example` files for examples.
with_config_schema?: false
]
]
]
end
defp adjust(release) do
for {app, config} <- release.applications,
auto_included_app?(app, config, release.options) do
dir = "#{app}-#{config[:vsn]}"
[release.path, "lib", dir]
|> Path.join()
|> File.rm_rf!()
end
# no need for the `releases` dir, but the dir itself must exist.
release.path
|> Path.join("releases")
|> File.rm_rf!()
release.path
|> Path.join("releases")
|> File.mkdir!()
release
end
defp emqx_tar(release) do
overwrite? = Keyword.get(release.options, :overwrite, false)
release_basename = "#{release.name}-#{release.version}"
readme_path = "README.md"
# include src files
include_src? = get_in(release.options, [:emqx_plugin_opts, :include_src?])
if include_src? do
for dir <- Mix.Project.config()[:elixirc_paths] do
File.cp_r!(
dir,
Path.join([
release.path,
"lib",
release_basename,
dir
])
)
end
end
lib_dirs =
Enum.reduce(release.applications, [], fn {app, config}, acc ->
if auto_included_app?(app, config, release.options) do
acc
else
dir = "#{app}-#{config[:vsn]}"
[dir | acc]
end
end)
readme =
if File.exists?(readme_path) do
name_in_tar =
release_basename
|> Path.join(readme_path)
|> to_charlist()
path_to_compress = to_charlist(readme_path)
[{name_in_tar, path_to_compress}]
else
[]
end
lib_files =
lib_dirs
|> Enum.filter(&File.exists?(Path.join([release.path, "lib", &1])))
|> Enum.map(fn filename ->
name_in_tar =
release_basename
|> Path.join(filename)
|> to_charlist()
path_to_compress =
[release.path, "lib", filename]
|> Path.join()
|> to_charlist()
{name_in_tar, path_to_compress}
end)
release_json_contents = make_plugin_release_json(release, lib_dirs)
release_json_path = Path.join([release.path, "release.json"])
Mix.Generator.create_file(release_json_path, release_json_contents, force: overwrite?)
release_json = [
{
to_charlist(Path.join(release_basename, "release.json")),
to_charlist(release_json_path)
}
]
files = lib_files ++ readme ++ release_json
out_dir =
Path.join([
Mix.Project.build_path(),
"plugrelex",
to_string(release.name)
])
Mix.Generator.create_directory(out_dir, force: overwrite?)
out_path = Path.join(out_dir, "#{release_basename}.tar.gz")
Mix.shell().info([:green, "* building ", :reset, out_path])
:ok = :erl_tar.create(to_charlist(out_path), files, [:dereference, :compressed])
release
end
defp make_plugin_release_json(release, lib_dirs) do
with_config_schema? = get_in(release.options, [:emqx_plugin_opts, :with_config_schema?])
extra_info = %{
name: release.name,
rel_vsn: release.version,
rel_apps: lib_dirs,
git_ref: git_ref(),
git_commit_or_build_date: get_date(),
metadata_vsn: @emqx_metadata_vsn,
built_on_otp_release: System.otp_release(),
built_on_elixir_release: System.version(),
with_config_schema: with_config_schema?
}
release.options[:emqx_plugin_info]
|> Map.merge(extra_info)
|> Jason.encode!(pretty: true)
end
defp git_ref() do
res = System.cmd("git", ["rev-parse", "HEAD"])
case res do
{ref, 0} ->
String.trim(ref)
{_, _} ->
"unknown"
end
end
defp get_date() do
res =
System.cmd(
"git",
["log", "-1", "--pretty=format:'%cd'", "--date=format:'%Y-%m-%d'"]
)
case res do
{date, 0} ->
String.trim(date)
{_, _} ->
to_string(Date.utc_today())
end
end
defp auto_included_app?(app, config, opts) do
exclude_elixir? = get_in(opts, [:emqx_plugin_opts, :exclude_elixir?])
(exclude_elixir? and app in [:elixir, :iex]) or config[:otp_app?]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
# emqx v5.7.0
emqx_ref = "d885ac3dd8626fb6ad3d246db279c922e4eb5a0c"
emqx_dep = fn app ->
{app,
git: "https://github.com/emqx/emqx",
ref: emqx_ref,
sparse: "apps/#{app}",
override: true,
runtime: false}
end
[
# This is just for building the tarball. Remove `runtime:
# false` if your application depends on this lib.
{:jason, "~> 1.3", runtime: false},
# If your plugin depends on emqx, you may include it here. Be
# sure to use `runtime: false`.
emqx_dep.(:emqx),
emqx_dep.(:emqx_ctl),
emqx_dep.(:emqx_utils),
emqx_dep.(:emqx_durable_storage),
## Uncomment the `:emqx_plugins` line if your application uses custom configuration
## schema:
# emqx_dep.(:emqx_plugins),
# temporarily needed due to clashing dependencies of
# dependencies of emqx.
{:cowlib, "2.8.0", override: true, runtime: false},
{:jiffy, github: "emqx/jiffy", tag: "1.0.6", override: true, runtime: false},
# These are dependencies for your plugin
{:hallux, "~> 1.2"}
]
end
end