Skip to content

Commit

Permalink
implement file string to pascal string variable
Browse files Browse the repository at this point in the history
  • Loading branch information
zamronypj committed Apr 9, 2021
1 parent 4f03201 commit 1dee6ad
Show file tree
Hide file tree
Showing 21 changed files with 268 additions and 64 deletions.
180 changes: 180 additions & 0 deletions src/Tasks/Implementations/Text2Inc/Text2IncTaskImpl.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
(*!------------------------------------------------------------
* Fano CLI Application (https://fanoframework.github.io)
*
* @link https://github.com/fanoframework/fano-cli
* @copyright Copyright (c) 2018 - 2020 Zamrony P. Juhara
* @license https://github.com/fanoframework/fano-cli/blob/master/LICENSE (MIT)
*------------------------------------------------------------- *)
unit Text2IncTaskImpl;

interface

{$MODE OBJFPC}
{$H+}

uses

TaskOptionsIntf,
TaskIntf,
Classes,
SysUtils;

type

(*!--------------------------------------
* Task that convert file string into
* pascal string variable
*---------------------------------------------
* If we have file with content as follows
* hello world
* It will create new file with content as follows
* varName : string =
* 'hello world';
*---------------------------------------------
* @credit https://github.com/zamronypj/txt2inc
* @author Zamrony P. Juhara <zamronypj@yahoo.com>
*---------------------------------------*)
TText2IncTask = class(TInterfacedObject, ITask)
private
function readStdIn() : string;
procedure txt2inc(
const src : TStrings;
const dst : TStrings;
const varName : string
);
public
function run(
const opt : ITaskOptions;
const longOpt : shortstring
) : ITask;
end;

implementation

uses

iostream,
StrFormats;

function TText2IncTask.readStdIn() : string;
var
buffer: string;
Count: Integer;
MyStream: TStringStream;
InputStream: TIOStream;
begin
MyStream := TStringStream.Create;
try
InputStream := TIOStream.Create(iosInput);
try
setLength(buffer, 4 * 1024);
repeat
Count := InputStream.read(buffer[1], Length(buffer));
if Count > 0 then
begin
MyStream.write(buffer[1], count);
end;
until Count = 0;
result := MyStream.dataString;
finally
InputStream.Free;
end;
finally
MyStream.Free;
end;
end;

procedure TText2IncTask.txt2inc(
const src : TStrings;
const dst : TStrings;
const varName : string
);
var i : integer;
begin
dst.add(varName + ' : string = ');

for i:=0 to src.count-2 do
begin
dst.add(' ' + QuotedStr(src[i]) + ' + LineEnding + ');
end;

if (src.count > 0) then
begin
dst.add(' ' + QuotedStr(src[src.count-1]) + ';');
end;
end;

function TText2IncTask.run(
const opt : ITaskOptions;
const longOpt : shortstring
) : ITask;
var srcFile : string;
dstFile : string;
varName : string;
src : TStrings;
dst : TStrings;
begin
result := self;
src:= TStringList.Create();
try
dst:= TStringList.Create();
try
srcFile := opt.getOptionValue('src');
if (srcFile = '') or (not opt.hasOption('src')) then
begin
src.text := readStdIn();
end else
begin
if not FileExists(srcFile) then
begin
writeln('Error. File ' + formatColor(srcFile, TXT_RED) + ' does not exist.');
exit;
end;
src.loadFromFile(srcFile);
end;


dstFile := opt.getOptionValue('dst');
if (dstFile = '') and (srcFile <> '') then
begin
dstFile := srcFile + '.inc';
end;

if (dstFile <> '') and (FileExists(dstFile)) and (not opt.hasOption('force')) then
begin
writeln(
'Error. File ' +
formatColor(dstFile, TXT_RED) +
' exists. Add ' +
formatColor('--force', TXT_GREEN) +
' to overwrite it.'
);
exit;
end;

varName := opt.getOptionValueDef('var', 'myStr');


txt2inc(src, dst, varName);

if (dstFile = '') then
begin
writeln();
writeln('{-------------begin----------------}');
write(dst.text);
writeln('{-------------end------------------}');
writeln();
end else
begin
dst.saveToFile(dstFile);
writeln('OK. ' + formatColor(dstFile, TXT_GREEN) + ' created');
end;

finally
dst.free();
end;
finally
src.free();
end;
end;
end.
4 changes: 2 additions & 2 deletions src/Tasks/Includes/Controller/task.registrations.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ try
appInst.registerTask(
'controller',
'--controller=[controller-name] Create new controller' + LineEnding +
' --route=[route pattern] if omited, controller name is assumed.' + LineEnding +
' --method=[request method] if omited, HTTP GET is assumed.',
' --route=[route pattern] if omitted, controller name is assumed.' + LineEnding +
' --method=[request method] if omitted, HTTP GET is assumed.',
taskFactory.build()
);
finally
Expand Down
6 changes: 3 additions & 3 deletions src/Tasks/Includes/Deploy/Cgi/task.registrations.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ try
appInst.registerTask(
'deploy-cgi',
'--deploy-cgi=[server name] Deploy as CGI web application.' + LineEnding +
' --web-server=[web server] if omited, apache is assumed.' + LineEnding +
' --doc-root=[document-root] if omited, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omited, 127.0.0.1 is assumed.',
' --web-server=[web server] if omitted, apache is assumed.' + LineEnding +
' --doc-root=[document-root] if omitted, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omitted, 127.0.0.1 is assumed.',
taskFactory.build()
);
finally
Expand Down
10 changes: 5 additions & 5 deletions src/Tasks/Includes/Deploy/Fcgi/task.registrations.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ try
appInst.registerTask(
'deploy-fcgi',
'--deploy-fcgi=[server name] Deploy as FastCGI web application with mod_proxy_fcgi.' + LineEnding +
' --web-server=[web server] if omited, apache is assumed.' + LineEnding +
' --doc-root=[document-root] if omited, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omited, 127.0.0.1 is assumed.' + LineEnding +
' --host=[host where FastCGI app listen] if omited, 127.0.0.1 is assumed.' + LineEnding +
' --port=[port where FastCGI app listen] if omited, 20477 is assumed.',
' --web-server=[web server] if omitted, apache is assumed.' + LineEnding +
' --doc-root=[document-root] if omitted, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omitted, 127.0.0.1 is assumed.' + LineEnding +
' --host=[host where FastCGI app listen] if omitted, 127.0.0.1 is assumed.' + LineEnding +
' --port=[port where FastCGI app listen] if omitted, 20477 is assumed.',
taskFactory.build()
);
finally
Expand Down
14 changes: 7 additions & 7 deletions src/Tasks/Includes/Deploy/FcgiBalancer/task.registrations.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ try
appInst.registerTask(
'deploy-lb-fcgi',
'--deploy-lb-fcgi=[server name] Deploy as FastCGI web application with mod_proxy_balancer.' + LineEnding +
' --members=[host1:port1,host2:port2,...], if omited, 2 default members is assumed.' + LineEnding +
' --lbmethod=[load balancing scheduler algorithm] if omited, byrequests is assumed.' + LineEnding +
' --web-server=[web server] if omited, apache is assumed.' + LineEnding +
' --doc-root=[document-root] if omited, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omited, 127.0.0.1 is assumed.' + LineEnding +
' --host=[host where FastCGI app listen] if omited, 127.0.0.1 is assumed.' + LineEnding +
' --port=[port where FastCGI app listen] if omited, 20477 is assumed.',
' --members=[host1:port1,host2:port2,...], if omitted, 2 default members is assumed.' + LineEnding +
' --lbmethod=[load balancing scheduler algorithm] if omitted, byrequests is assumed.' + LineEnding +
' --web-server=[web server] if omitted, apache is assumed.' + LineEnding +
' --doc-root=[document-root] if omitted, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omitted, 127.0.0.1 is assumed.' + LineEnding +
' --host=[host where FastCGI app listen] if omitted, 127.0.0.1 is assumed.' + LineEnding +
' --port=[port where FastCGI app listen] if omitted, 20477 is assumed.',
taskFactory.build()
);
finally
Expand Down
4 changes: 2 additions & 2 deletions src/Tasks/Includes/Deploy/Fcgid/task.registrations.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ try
appInst.registerTask(
'deploy-fcgid',
'--deploy-fcgid=[server name] Deploy as FastCGI web application with mod_fcgid.' + LineEnding +
' --doc-root=[document-root] if omited, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omited, 127.0.0.1 is assumed.',
' --doc-root=[document-root] if omitted, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omitted, 127.0.0.1 is assumed.',
taskFactory.build()
);
finally
Expand Down
10 changes: 5 additions & 5 deletions src/Tasks/Includes/Deploy/Http/task.registrations.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ try
appInst.registerTask(
'deploy-http',
'--deploy-http=[server name] Deploy as http web application over http reverse proxy server.' + LineEnding +
' --web-server=[web server] if omited, apache is assumed.' + LineEnding +
' --doc-root=[document-root] if omited, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omited, 127.0.0.1 is assumed.' + LineEnding +
' --host=[host where http app listen] if omited, 127.0.0.1 is assumed.' + LineEnding +
' --port=[port where http app listen] if omited, 20477 is assumed.',
' --web-server=[web server] if omitted, apache is assumed.' + LineEnding +
' --doc-root=[document-root] if omitted, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omitted, 127.0.0.1 is assumed.' + LineEnding +
' --host=[host where http app listen] if omitted, 127.0.0.1 is assumed.' + LineEnding +
' --port=[port where http app listen] if omitted, 20477 is assumed.',
taskFactory.build()
);
finally
Expand Down
14 changes: 7 additions & 7 deletions src/Tasks/Includes/Deploy/HttpBalancer/task.registrations.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ try
appInst.registerTask(
'deploy-lb-http',
'--deploy-lb-http=[server name] Deploy as http web application with mod_proxy_balancer.' + LineEnding +
' --members=[host1:port1,host2:port2,...], if omited, 2 default members is assumed.' + LineEnding +
' --lbmethod=[load balancing scheduler algorithm] if omited, byrequests is assumed.' + LineEnding +
' --web-server=[web server] if omited, apache is assumed.' + LineEnding +
' --doc-root=[document-root] if omited, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omited, 127.0.0.1 is assumed.' + LineEnding +
' --host=[host where http app listen] if omited, 127.0.0.1 is assumed.' + LineEnding +
' --port=[port where http app listen] if omited, 20477 is assumed.',
' --members=[host1:port1,host2:port2,...], if omitted, 2 default members is assumed.' + LineEnding +
' --lbmethod=[load balancing scheduler algorithm] if omitted, byrequests is assumed.' + LineEnding +
' --web-server=[web server] if omitted, apache is assumed.' + LineEnding +
' --doc-root=[document-root] if omitted, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omitted, 127.0.0.1 is assumed.' + LineEnding +
' --host=[host where http app listen] if omitted, 127.0.0.1 is assumed.' + LineEnding +
' --port=[port where http app listen] if omitted, 20477 is assumed.',
taskFactory.build()
);
finally
Expand Down
10 changes: 5 additions & 5 deletions src/Tasks/Includes/Deploy/Scgi/task.registrations.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ try
appInst.registerTask(
'deploy-scgi',
'--deploy-scgi=[server name] Deploy as SCGI web application.' + LineEnding +
' --web-server=[web server] if omited, apache is assumed.' + LineEnding +
' --doc-root=[document-root] if omited, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omited, 127.0.0.1 is assumed.' + LineEnding +
' --host=[host where SCGI app listen] if omited, 127.0.0.1 is assumed.' + LineEnding +
' --port=[port where SCGI app listen] if omited, 20477 is assumed.',
' --web-server=[web server] if omitted, apache is assumed.' + LineEnding +
' --doc-root=[document-root] if omitted, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omitted, 127.0.0.1 is assumed.' + LineEnding +
' --host=[host where SCGI app listen] if omitted, 127.0.0.1 is assumed.' + LineEnding +
' --port=[port where SCGI app listen] if omitted, 20477 is assumed.',
taskFactory.build()
);
finally
Expand Down
14 changes: 7 additions & 7 deletions src/Tasks/Includes/Deploy/ScgiBalancer/task.registrations.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ try
appInst.registerTask(
'deploy-lb-scgi',
'--deploy-lb-scgi=[server name] Deploy as SCGI web application with mod_proxy_balancer.' + LineEnding +
' --members=[host1:port1,host2:port2,...], if omited, 2 default members is assumed.' + LineEnding +
' --lbmethod=[load balancing scheduler algorithm] if omited, byrequests is assumed.' + LineEnding +
' --web-server=[web server] if omited, apache is assumed.' + LineEnding +
' --doc-root=[document-root] if omited, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omited, 127.0.0.1 is assumed.' + LineEnding +
' --host=[host where SCGI app listen] if omited, 127.0.0.1 is assumed.' + LineEnding +
' --port=[port where SCGI app listen] if omited, 20477 is assumed.',
' --members=[host1:port1,host2:port2,...], if omitted, 2 default members is assumed.' + LineEnding +
' --lbmethod=[load balancing scheduler algorithm] if omitted, byrequests is assumed.' + LineEnding +
' --web-server=[web server] if omitted, apache is assumed.' + LineEnding +
' --doc-root=[document-root] if omitted, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omitted, 127.0.0.1 is assumed.' + LineEnding +
' --host=[host where SCGI app listen] if omitted, 127.0.0.1 is assumed.' + LineEnding +
' --port=[port where SCGI app listen] if omitted, 20477 is assumed.',
taskFactory.build()
);
finally
Expand Down
10 changes: 5 additions & 5 deletions src/Tasks/Includes/Deploy/Uwsgi/task.registrations.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ try
appInst.registerTask(
'deploy-uwsgi',
'--deploy-uwsgi=[server name] Deploy as uwsgi web application.' + LineEnding +
' --web-server=[web server] if omited, apache is assumed.' + LineEnding +
' --doc-root=[document-root] if omited, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omited, 127.0.0.1 is assumed.' + LineEnding +
' --host=[host where uwsgi app listen] if omited, 127.0.0.1 is assumed.' + LineEnding +
' --port=[port where uwsgi app listen] if omited, 20477 is assumed.',
' --web-server=[web server] if omitted, apache is assumed.' + LineEnding +
' --doc-root=[document-root] if omitted, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omitted, 127.0.0.1 is assumed.' + LineEnding +
' --host=[host where uwsgi app listen] if omitted, 127.0.0.1 is assumed.' + LineEnding +
' --port=[port where uwsgi app listen] if omitted, 20477 is assumed.',
taskFactory.build()
);
finally
Expand Down
14 changes: 7 additions & 7 deletions src/Tasks/Includes/Deploy/UwsgiBalancer/task.registrations.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ try
appInst.registerTask(
'deploy-lb-uwsgi',
'--deploy-lb-uwsgi=[server name] Deploy as uwsgi web application with mod_proxy_balancer.' + LineEnding +
' --members=[host1:port1,host2:port2,...], if omited, 2 default members is assumed.' + LineEnding +
' --lbmethod=[load balancing scheduler algorithm] if omited, byrequests is assumed.' + LineEnding +
' --web-server=[web server] if omited, apache is assumed.' + LineEnding +
' --doc-root=[document-root] if omited, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omited, 127.0.0.1 is assumed.' + LineEnding +
' --host=[host where uwsgi app listen] if omited, 127.0.0.1 is assumed.' + LineEnding +
' --port=[port where uwsgi app listen] if omited, 20477 is assumed.',
' --members=[host1:port1,host2:port2,...], if omitted, 2 default members is assumed.' + LineEnding +
' --lbmethod=[load balancing scheduler algorithm] if omitted, byrequests is assumed.' + LineEnding +
' --web-server=[web server] if omitted, apache is assumed.' + LineEnding +
' --doc-root=[document-root] if omitted, public directory in current directory is used.' + LineEnding +
' --server-ip=[web server ip] if omitted, 127.0.0.1 is assumed.' + LineEnding +
' --host=[host where uwsgi app listen] if omitted, 127.0.0.1 is assumed.' + LineEnding +
' --port=[port where uwsgi app listen] if omitted, 20477 is assumed.',
taskFactory.build()
);
finally
Expand Down
2 changes: 1 addition & 1 deletion src/Tasks/Includes/Minifier/Css/task.registrations.inc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ try
'cssmin',
'--cssmin=[path] Minify CSS file. Path can be file path or directory.' + LineEnding +
' --output=[output path] target output path' + LineEnding +
' if omited, it is assumed [path].min.css' + LineEnding +
' if omitted, it is assumed [path].min.css' + LineEnding +
' if output-path = stdout, minified version is output to STDOUT',
taskFactory.build()
);
Expand Down
2 changes: 1 addition & 1 deletion src/Tasks/Includes/Minifier/Js/task.registrations.inc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ try
'jsmin',
'--jsmin=[path] Minify JavaScript file. Path can be file path or directory.' + LineEnding +
' --output=[output path] target output path' + LineEnding +
' if omited, it is assumed [path].min.js' + LineEnding +
' if omitted, it is assumed [path].min.js' + LineEnding +
' if output-path = stdout, minified version is output to STDOUT',
taskFactory.build()
);
Expand Down
2 changes: 1 addition & 1 deletion src/Tasks/Includes/Project/FastCgi/task.registrations.inc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ try
appInst.registerTask(
'project-fcgi',
'--project-fcgi=[project-name] Create new FastCGI project.' + LineEnding +
' --config=[ini, json] if omited, config is not setup.' + LineEnding +
' --config=[ini, json] if omitted, config is not setup.' + LineEnding +
' --host=[host where FastCGI app listen] if omitted, 127.0.0.1 is assumed.' + LineEnding +
' --port=[port where FastCGI app listen] if omitted, 20477 is assumed.' + LineEnding +
' --with-session=[file, cookie, db] Add session support' + LineEnding +
Expand Down
Loading

0 comments on commit 1dee6ad

Please sign in to comment.