Skip to content

Commit 4a7ed61

Browse files
committed
Formatting of update-swagger-url
1 parent 531d223 commit 4a7ed61

2 files changed

Lines changed: 38 additions & 37 deletions

File tree

priv/update-swagger-url

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
1-
#! /usr/bin/env escript
2-
1+
#!/usr/bin/env escript
32
-module('update-swagger-url').
43

54
main(_) ->
6-
%% Define the path to the template and the output file
7-
TemplateFilePath = "priv/swagger/swagger-initializer.template",
8-
OutputFilePath = "priv/swagger/swagger-initializer.js",
9-
10-
%% Read the contents of the template file
11-
case file:read_file(TemplateFilePath) of
12-
{ok, Content} ->
13-
%% Get the configuration for the path from the application environment
14-
Path = application:get_env(cowboy_swagger, path, "/api-docs"),
15-
16-
%% Ensure Path is a binary (if it's a list, convert to binary)
17-
BinPath = case is_binary(Path) of
18-
true -> Path;
19-
false -> list_to_binary(Path)
20-
end,
21-
22-
%% Replace the placeholder in the template file content
23-
%% Ensure both the placeholder and replacement are binaries
24-
ModifiedContent = binary:replace(Content, <<"__SWAGGER_PATH__">>, <<"'", BinPath/binary, "'">>, [global]),
25-
26-
%% Write the modified content to the new JavaScript file
27-
case file:write_file(OutputFilePath, ModifiedContent) of
28-
ok ->
29-
%% Successfully written, log the updated path
30-
io:format("Swagger path updated to: ~s~n", [Path]);
31-
{error, WriteError} ->
32-
%% Failed to write the new file
33-
io:format("Failed to write swagger-initializer.js: ~p~n", [WriteError])
34-
end;
35-
36-
{error, ReadError} ->
37-
%% Failed to read the template file
38-
io:format("Failed to read swagger-initializer.template: ~p~n", [ReadError])
39-
end.
5+
%% Define the path to the template and the output file
6+
TemplateFilePath = "priv/swagger/swagger-initializer.template",
7+
OutputFilePath = "priv/swagger/swagger-initializer.js",
8+
%% Read the contents of the template file
9+
case file:read_file(TemplateFilePath) of
10+
{ok, Content} ->
11+
%% Get the configuration for the path from the application environment
12+
Path = application:get_env(cowboy_swagger, path, "/api-docs"),
13+
%% Ensure Path is a binary (if it's a list, convert to binary)
14+
BinPath =
15+
case is_binary(Path) of
16+
true ->
17+
Path;
18+
false ->
19+
list_to_binary(Path)
20+
end,
21+
%% Replace the placeholder in the template file content
22+
%% Ensure both the placeholder and replacement are binaries
23+
ModifiedContent =
24+
binary:replace(Content,
25+
<<"__SWAGGER_PATH__">>,
26+
<<"'", BinPath/binary, "'">>,
27+
[global]),
28+
%% Write the modified content to the new JavaScript file
29+
case file:write_file(OutputFilePath, ModifiedContent) of
30+
ok ->
31+
%% Successfully written, log the updated path
32+
io:format("Swagger path updated to: ~s~n", [Path]);
33+
{error, WriteError} ->
34+
%% Failed to write the new file
35+
io:format("Failed to write swagger-initializer.js: ~p~n", [WriteError])
36+
end;
37+
{error, ReadError} ->
38+
%% Failed to read the template file
39+
io:format("Failed to read swagger-initializer.template: ~p~n", [ReadError])
40+
end.

rebar.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
%% == Format ==
4444

45-
{format, [{files, ["*.config", "src/*", "test/*"]}]}.
45+
{format, [{files, ["*.config", "src/*", "test/*", "priv/update-swagger-url"]}]}.
4646

4747
%% == Dialyzer + XRef ==
4848

0 commit comments

Comments
 (0)