NppExec compilation of AutoIt files, with includes...
-
Is there a way that the NppExec script would work from any file of my project ? that it could know the mainfile of my project ?
Maybe with some kind of NPP environnement variable that I could set at the beginning of each file of my project ?I am not sure how you expect NppExec would parse your active file – unlike some of the other scripting language plugins, NppExec doesn’t really have easy access to whatever portion of the current file you want. So I don’t think it would be easy to implement something like that.
In the end, you would have to have some way of telling NppExec what the “main” file is for each project. And since NppExec is more filesystem-focused than file-content-focused, I would suggest a specialized file for the purpose.
In many software development environments, there is something called a “makefile”, where you define how to compile and/or test and/or otherwise “make” a project – and each project has its own makefile. It could be a literal makefile, which works with the make/gmake/nmake utility on your computer; or it could be a batchfile; or it could be some custom solution.
So for your case, I might suggest creating a file called
makefile.nppexec
, which goes in the same folder as your .au3 files for a given project. Themakefile.nppexec
would look something likeset PROJECTMAINFILE = PutMainFileNameHere.au3 echo Running main program $PROJECTMAINFILE from directory $(CURRENT_DIRECTORY) "C:\Program Files (x86)\AutoIt3\Au3check.exe" "$(CURRENT_DIRECTORY)\$(PROJECTMAINFILE)" if $(EXITCODE) >= 2 goto EXITSCRIPT "C:\Program Files (x86)\AutoIt3\AutoIt3.exe" /ErrorStdOut "$(CURRENT_DIRECTORY)\$(PROJECTMAINFILE)" :EXITSCRIPT
… and you would customize the makefile.nppexec in each project to call that project’s main file (using the variable at the top)
Then you would change your main NppExec script to:
npp_saveall echo Starting from : $(FILE_NAME) cd "$(CURRENT_DIRECTORY)" if $(EXT_PART) != .au3 goto BADFILE npp_exec makefile.nppexec // this runs the "makefile" for the current project goto EXITSCRIPT :BADFILE echo "Not an autoit file !!!" :EXITSCRIPT
… so you would have one
makefile.nppexec
per project folder, and you would then run the central NppExec script, and based on the current directory of the active .au3 file, it would run the appropriate makefile.nppexec from that folder. -
@PeterJones Thank you for your very constructive solution !
I will integrate it right now. -
@PeterJones I just wonder if the variable PROJECTMAINFILE defined in the makefile.nppexec script would be visible in the common NppExec script ? Infact I wonder if the makefile could only contain this variable declaration and put the rest back in the main NppExec script ? I suppose it does not work like an “include”. Or maybe Environement variables are “remanent” ?
-
@Dave-Binbin said in NppExec compilation of AutoIt files, with includes...:
Is there a way that the NppExec script would work from any file of my project ?
You could set a NppExec global variable to the project root directory and then at the beginning of your compile script, just CD to that directory. I do something very similar - I have a “project” NppExec script which sets a project directory as an NppExec global variable and then all of my other scripts can see / use that directory (e.g., CD to it) to do stuff. So I can set my project directory to the one with the
Makefile
and then press my compile shortcut hotkey for my NppExec compile script from the header file in a different directory and it will compile the whole project.Cheers.
-
@Michael-Vincent I do not understand how you implement this… By global NppExec variable, do you mean env_set variable ? Could you post a example ?
-
@Dave-Binbin said in NppExec compilation of AutoIt files, with includes...:
By global NppExec variable, do you mean env_set variable ? Could you post a example ?
Exactly:
ENV_SET PROJECT_DIR=C:\my\proj\dir
This way, if I spawn a shell (i.e., launch Command Prompt from NppExec) - that environment variable is present there as well.
Cheers.
-
@Michael-Vincent as I will not define a “makefile” in every project, I would like to compile by default the current NPP file $(FULL_CURRENT_PATH). So I wonder how I could test if the env variable exists or not ?
if "$(SYS.PROJECTMAINFILE)" != "" then
crashes if the env var is not defined…
-
In your main script, start with
ENV_SET PROJECTMAINFILE="$(FULL_CURRENT_PATH)"
then call the makefile – if the makefile defines that same variable, the value will be updated to whatever’s in the makefile, otherwise it will use the initial value from the main script.
(There might even be a way in NppExec to say “if file makefile.nppexec exists then npp_exec that file”, but I don’t know how to do file-exist tests in NppExec. @Michael-Vincent might.)
-
@Dave-Binbin said in NppExec compilation of AutoIt files, with includes...:
By global NppExec variable
local vs global vs environment
set local myvar=value
– only seen in the active scriptset myglobal=value
– seen in the active script and in subsequent scripts or the Consoleenv_set myenv=value
– seen in the active script, subsequent scripts, and the console as $(SYS.MYENV); seen in any subshells (like cmd.exe or powershell called from NppExec) as a normal environment variable, using appropriate shell syntax
-
@PeterJones said in NppExec compilation of AutoIt files, with includes...:
ENV_SET PROJECTMAINFILE=“$(FULL_CURRENT_PATH)”
great, thanks !!!
do you know how I can test the file extension “.au3” of this file like I did in the original script,
in case there is no makefile and I point on a non-au3 file…if $(EXT_PART) != .au3 goto BADFILE
-
do you know how I can test the file extension “.au3” of this file like I did in the original script,
By doing it like you did in the original script, or like I showed in my original “main NppExec script”.
main script
npp_saveall ENV_SET PROJECTMAINFILE=$(FULL_CURRENT_PATH) echo Starting from : $(FILE_NAME) cd "$(CURRENT_DIRECTORY)" if $(EXT_PART) != .au3 goto BADFILE npp_exec makefile.nppexec // this runs the "makefile" for the current project "C:\Program Files (x86)\AutoIt3\Au3check.exe" "$(SYS.PROJECTMAINFILE)" if $(EXITCODE) >= 2 goto EXITSCRIPT "C:\Program Files (x86)\AutoIt3\AutoIt3.exe" /ErrorStdOut "$(SYS.PROJECTMAINFILE)" goto EXITSCRIPT :BADFILE echo "Not an autoit file !!!" :EXITSCRIPT
makefile.nppexec
ENV_SET PROJECTMAINFILE=MyNameHere.au3
—
EDIT: removed the quotes from the env_set, which puts quotes in the string -
@PeterJones said in NppExec compilation of AutoIt files, with includes...:
EXT_PART
but in case the makefile exists, EXT_PART will not test the updated PROJECTMAINFILE but still the file from which I run the script, no ?
-
@Dave-Binbin said in NppExec compilation of AutoIt files, with includes...:
but in case the makefile exists, EXT_PART will not test the updated PROJECTMAINFILE but still the file from which I run the script, no ?
Correct, $(EXT_PART) is always the extension portion of the active file in Notepad++, and it is not affected by the makefile at all.
So I may have misunderstood your original follow-on request. I think the best advice is “don’t set PROJECTMAINFILE to a non-.au3 file”.
Using the substring notation (see
help set
or Plugins > NppExec > Docs…) for variable setting, you might be able to extract the last three characters from $(SYS.PROJECTMAINFILE) into a separate variable, and use that for testing whether or not to skip. But that would take some research and experimenting; I think you should be able to read those documents as well as I can. -
I got curious, and had a couple spare minutes, so I did the experiment.
set local e ~ substr -4 4 $(SYS.PROJECTMAINFILE) if $(e) != .au3 goto BADFILE
so if you put that immediately after the
npp_exec makefile.nppexec
and before the Au3check.exe call, it will do a test for the PROJECTMAINFILE extension as well.