From 23b965269ea293ce33598c6eebe2321b0ef5260d Mon Sep 17 00:00:00 2001 From: vk Date: Wed, 9 Oct 2024 16:14:32 +0300 Subject: [PATCH] Formatting of update-swagger-url --- priv/update-swagger-url | 71 +++++++++++++++++++++-------------------- rebar.config | 2 +- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/priv/update-swagger-url b/priv/update-swagger-url index 8cc6196..918ff03 100755 --- a/priv/update-swagger-url +++ b/priv/update-swagger-url @@ -1,39 +1,42 @@ -#! /usr/bin/env escript - +#!/usr/bin/env escript -module('update-swagger-url'). main(_) -> - %% Define the path to the template and the output file - TemplateFilePath = "priv/swagger/swagger-initializer.template", - OutputFilePath = "priv/swagger/swagger-initializer.js", - - %% Read the contents of the template file - case file:read_file(TemplateFilePath) of - {ok, Content} -> - %% Get the configuration for the path from the application environment - Path = application:get_env(cowboy_swagger, path, "/api-docs"), - - %% Ensure Path is a binary (if it's a list, convert to binary) - BinPath = case is_binary(Path) of - true -> Path; - false -> list_to_binary(Path) - end, - - %% Replace the placeholder in the template file content - %% Ensure both the placeholder and replacement are binaries - ModifiedContent = binary:replace(Content, <<"__SWAGGER_PATH__">>, <<"'", BinPath/binary, "'">>, [global]), + %% Define the path to the template and the output file + TemplateFilePath = "priv/swagger/swagger-initializer.template", + OutputFilePath = "priv/swagger/swagger-initializer.js", + %% Read the contents of the template file + case file:read_file(TemplateFilePath) of + {ok, Content} -> + %% Get the configuration for the path from the application environment + Path = application:get_env(cowboy_swagger, path, "/api-docs"), + %% Ensure Path is a binary (if it's a list, convert to binary) + BinPath = + case is_binary(Path) of + true -> + Path; + false -> + list_to_binary(Path) + end, - %% Write the modified content to the new JavaScript file - case file:write_file(OutputFilePath, ModifiedContent) of - ok -> - %% Successfully written, log the updated path - io:format("Swagger path updated to: ~s~n", [Path]); - {error, WriteError} -> - %% Failed to write the new file - io:format("Failed to write swagger-initializer.js: ~p~n", [WriteError]) - end; + %% Replace the placeholder in the template file content + %% Ensure both the placeholder and replacement are binaries + ModifiedContent = + binary:replace(Content, + <<"__SWAGGER_PATH__">>, + <<"'", BinPath/binary, "'">>, + [global]), - {error, ReadError} -> - %% Failed to read the template file - io:format("Failed to read swagger-initializer.template: ~p~n", [ReadError]) - end. + %% Write the modified content to the new JavaScript file + case file:write_file(OutputFilePath, ModifiedContent) of + ok -> + %% Successfully written, log the updated path + io:format("Swagger path updated to: ~s~n", [Path]); + {error, WriteError} -> + %% Failed to write the new file + io:format("Failed to write swagger-initializer.js: ~p~n", [WriteError]) + end; + {error, ReadError} -> + %% Failed to read the template file + io:format("Failed to read swagger-initializer.template: ~p~n", [ReadError]) + end. diff --git a/rebar.config b/rebar.config index c1df1be..1e2d4ec 100644 --- a/rebar.config +++ b/rebar.config @@ -42,7 +42,7 @@ %% == Format == -{format, [{files, ["*.config", "src/*", "test/*"]}]}. +{format, [{files, ["*.config", "src/*", "test/*", "priv/update-swagger-url"]}]}. %% == Dialyzer + XRef ==