|
1 | | -#! /usr/bin/env escript |
2 | | - |
| 1 | +#!/usr/bin/env escript |
3 | 2 | -module('update-swagger-url'). |
4 | 3 |
|
5 | 4 | 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. |
0 commit comments