Need help creating a post-build command

This forum is for all Flare issues not related to any of the other categories.
Post Reply
doc_guy
Propellus Maximus
Posts: 1979
Joined: Tue Nov 28, 2006 11:18 am
Location: Crossroads of the West
Contact:

Need help creating a post-build command

Post by doc_guy »

I'm wondering if anybody can help me with creating a post-build command. Here is the scenario:

I'm building my topnav target with a custom extension: .PHP. I need to open every PHP file in the output folder, and insert a PHP command as the first line in the file. Currently, after I do a PHP build, I open one of the files in Notepad++, I do a "Find and Replace In Files". I search for:

<!DOCTYPE html>

And replace it with

<?php include ($_SERVER['DOCUMENT_ROOT'].'/scripts/session.php'); ?><!DOCTYPE html>

Basically, we need to protect each page on the site so that it only loads if the user is logged in to the system. This is checked in the session.php file.

Anyway, I'd like to do a post-build command that does this for me, but I don't know how to write the script that will search for PHP files, look for the DocType element, and insert the PHP script before the DocType element.

Can anybody help me with that build command?
Paul Pehrson
My Blog

Image
GregStenhouse
Sr. Propeller Head
Posts: 330
Joined: Tue May 13, 2008 3:27 pm
Location: Christchurch, New Zealand

Re: Need help creating a post-build command

Post by GregStenhouse »

Probably easiest using Powershell. This is similar to the approach we use and should loop through PHP files searching and replacing the text you want. post_publish_script.cmd and post_publish_script.ps1 are in the high level directory next to your project file.

Build Command in Flare:

Code: Select all

$(ProjectDirectory)post_publish_script.cmd" "$(OutputDirectory)"
post_publish_script.cmd:

Code: Select all

@echo off
powershell.exe -executionpolicy remotesigned -Command "& '%~dpn0.ps1'" -filePath '%1'
PAUSE
post_publish_script.ps1:

Code: Select all

param( [string]$filePath )
$filePath
$phpFiles = (Get-ChildItem $filePath -Filter *.php)
for ($i=0; $i -lt $phpFiles.Count; $i++) {
(Get-Content $phpFiles[$i].FullName) | Foreach-Object {
    $_ -replace '<!DOCTYPE html>', '<?php include ($_SERVER[''DOCUMENT_ROOT''].''/scripts/session.php''); ?><!DOCTYPE html>'
} | Set-Content $phpFiles[$i].FullName;
}
doc_guy
Propellus Maximus
Posts: 1979
Joined: Tue Nov 28, 2006 11:18 am
Location: Crossroads of the West
Contact:

Re: Need help creating a post-build command

Post by doc_guy »

Thanks, Greg! I'll try this.

-Paul
Paul Pehrson
My Blog

Image
doc_guy
Propellus Maximus
Posts: 1979
Joined: Tue Nov 28, 2006 11:18 am
Location: Crossroads of the West
Contact:

Re: Need help creating a post-build command

Post by doc_guy »

First, is there a missing quotation at the beginning of the post_publish_script.cmd file above?

Second, I get the following build errors when I add the quotation mark I think is missing:

Code: Select all

6/29/2017 4:24:32 PM - Done
 
6/29/2017 4:24:32 PM - Running Post-Build Event Command...
 
6/29/2017 4:24:33 PM - Run Command Error: Get-ChildItem : Illegal characters in path.
 
6/29/2017 4:24:33 PM - Run Command Error: At C:\GIT\Documentation\TPP Project\post_publish_script.ps1:3 char:14
 
6/29/2017 4:24:33 PM - Run Command Error: + $phpFiles = (Get-ChildItem $filePath -Filter *.php)
 
6/29/2017 4:24:33 PM - Run Command Error: +              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
6/29/2017 4:24:33 PM - Run Command Error:     + CategoryInfo          : InvalidArgument: (C:\GIT\Document...TopNav_CS-Site":String) [Get-ChildItem], ArgumentExc 
 
6/29/2017 4:24:33 PM - Run Command Error:    eption
 
6/29/2017 4:24:33 PM - Run Command Error:     + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand
 
6/29/2017 4:24:33 PM - Run Command Error:  
 
6/29/2017 4:24:33 PM - Run Command Error: Get-ChildItem : Cannot find path 'C:\GIT\Documentation\TPP Project\Output\paul_pehrson\CS-Site\TopNav_CS-Site"' 
 
6/29/2017 4:24:33 PM - Run Command Error: because it does not exist.
 
6/29/2017 4:24:33 PM - Run Command Error: At C:\GIT\Documentation\TPP Project\post_publish_script.ps1:3 char:14
 
6/29/2017 4:24:33 PM - Run Command Error: + $phpFiles = (Get-ChildItem $filePath -Filter *.php)
 
6/29/2017 4:24:33 PM - Run Command Error: +              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
6/29/2017 4:24:33 PM - Run Command Error:     + CategoryInfo          : ObjectNotFound: (C:\GIT\Document...TopNav_CS-Site":String) [Get-ChildItem], ItemNotFound 
 
6/29/2017 4:24:33 PM - Run Command Error:    Exception
 
6/29/2017 4:24:33 PM - Run Command Error:     + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
 
6/29/2017 4:24:33 PM - Run Command Error:  
 
6/29/2017 4:24:33 PM - Post-Build Event Command Done
 
6/29/2017 4:24:33 PM - Saving log to "C:\GIT\Documentation\TPP Project\Output\paul_pehrson\CS-Site\TopNav_CS-Site\TopNav_CS-Site.mclog"
 
Paul Pehrson
My Blog

Image
GregStenhouse
Sr. Propeller Head
Posts: 330
Joined: Tue May 13, 2008 3:27 pm
Location: Christchurch, New Zealand

Re: Need help creating a post-build command

Post by GregStenhouse »

You are right about needing the initial quotes, sorry about that.

I am guessing one or more PHP files has a reserved character in the filepath. Try using -literalPath

Code: Select all

param( [string]$filePath )
$filePath
$phpFiles = (Get-ChildItem $filePath -Filter *.php)
for ($i=0; $i -lt $phpFiles.Count; $i++) {
(Get-Content -literalPath $phpFiles[$i].FullName) | Foreach-Object {
    $_ -replace '<!DOCTYPE html>', '<?php include ($_SERVER[''DOCUMENT_ROOT''].''/scripts/session.php''); ?><!DOCTYPE html>'
} | Set-Content -literalPath $phpFiles[$i].FullName;
}
OS_Loc
Jr. Propeller Head
Posts: 6
Joined: Tue Feb 19, 2019 1:53 am

Re: Need help creating a post-build command

Post by OS_Loc »

Hi,

I'm trying to accomplish a similar thing as Paul. I'm building an HTML5 output and need to loop through all *.html files to add one line of code in a post-build event.
One of our developers has written a power shell script for this. However, I have no clue how to get Flare to execute it.

If I simply enter the path to the ps1 file in the post-build events field in my target, the file is merely opened after the build is completed. But nothing else happens.
Since we use global project linking, I cannot put this file on the highest level in my project folder. It is now in the Resources/Scripts folder.

So what exactly do I write into the post-build event field and do I need any additional script files to make this work?

Any help would be greatly appreciated.

Thanks and best regards,
Agnieszka
Alan Williamson
Jr. Propeller Head
Posts: 4
Joined: Mon Sep 17, 2012 4:01 am
Location: Melbourne, Australia

Re: Need help creating a post-build command

Post by Alan Williamson »

Hi - the correct formatting of Pre/Post-event commands can be challenging. I have been trying for a week or more to solve my problem with limited success.

The manuals have simplistic examples, for instance to create a folder the command is: mkdir C:\Users\myusername\Desktop\MyDirectory
and to use a batch command to do this: C:/Users/myusername/Desktop/Create_Directory.bat

To pass an argument to a program it should be: C:/Users/myusername/Desktop/Program_to_execute argument

If the path to the executable or argument have embedded spaces then they must have quotes around them otherwise the text after the space(s) will be taken as additional arguments, that is we need: "C:/Users/myusername/my folder/Program_to_execute" "argument with space(s)"

However if both the path/executable AND the argument(s) have spaces in them I believe you need additional quotes around the whole command - see https://stackoverflow.com/questions/188 ... e-argument
Referring to that post, I had success with the CALL variant but it pops up a command window for each execution. I have not managed to make the 'cmd' variant (which uses /c to suppress the pop-up) work yet, but I am hopeful!

FWIW, the following is what I am currently trying - for now I am concatenating all of the arguments with a '|' into one for testing.

"D:/Users/Alan/Documents/Flare/Senetas Encryption/Project/Flare_MyProgram.exe" "Melbourne|Documentation\International Guides\Int/Chapters/CLI Reference.fltar|pdf|n"

This needs a fuller explanation in the manuals and I'd appreciate any thoughts others might have re how this should be handled.
Post Reply