Here's the source...
Code:
#SingleInstance, Force
; Init
;Change WHICH file(s) included --> recreate ahk
;Change CONTENT of file(s) included --> a) Interpreted > (do nout)
; b) Compiled > Recompile exe
; @MyWorkingDir:
; Sets the "working directory" of the running script
MyWorkingDir = %A_ScriptDir%
MainFolder = %A_ScriptDir%
; @IncludeOrCombine:
; Determines if the output script will be
; a list of "#Include"d files, or
; a single file with the text from all files
; combined.
IncludeOrCombine = INCLUDE
;IncludeOrCombine = COMBINE
include_count = 0
TempFileName = %A_Temp%\AutoInclude.ahk
; End of the customizable section
; Main
;;FileSelectFolder, IncludeFolders1, *%MainFolder%, 2, Prompt] ;can't select >1 folders
; Display main Gui
Gui, Add, Button, x6 y9 w180 h40 gNew, New
Gui, Add, Button, x186 y9 w180 h40 gOpen, Load Set
Gui, Add, Button, x366 y9 w180 h40 gSave, Save Set
Gui, Add, Button, x546 y9 w180 h40 gSaveAs, Save Set As
Gui, Add, Button, x730 y9 w180 h40 gQuit, Quit ;was x726
;;Gui, Add, Button, x6 y60 w180 h40 gEdit, Edit Set
Gui, Add, Button, x6 y60 w180 h40 gBuild, Build
Gui, Show, w918 h108, Include Set Editor ;was w952
While build_it <> 1
Sleep, 100
;;return
;Gui, Submit [, NoHide]
;Let's collect all the Auto-Execute lines from each file,
;as well as all the code from each file,
;and create a new script that includes all of them!
;We have to "Split" the folders list for each line:
inc_folder_count = 0
Loop, Parse, IncludeFolders, `n
{
inc_folder_count++
IncludeFolder := A_LoopField
; Check that <IncludeFolder> exists
IfNotExist, %IncludeFolder%
{
MsgBox, The specified Include folder `n`n %IncludeFolder% `n`n does not exist!
ExitApp
}
; #Include folder
Include_folder_text%inc_folder_count% =
(ltrim
#Include %IncludeFolder%
)
;Let's load all the files from the folder:
;; SetWorkingDir %IncludeFolder%
Gosubs_text = ;for all files within this include folder
Labels_text = ; " " " ;Labels_Includes_Contents_text
Loop %IncludeFolder%\*.ahk, 0, 1 ;recursive
{
; Ignore's
; Ignore any files with "exclude" in the title:
If RegexMatch(A_LoopFileName, "i)\bexclude\b")
continue
; Ignore any files with "_backup" in the title:
If RegexMatch(A_LoopFileName, "i)_backup")
continue
; We are going to create an "#Include" directive for each file
; FILE-ALTERING STUFF
; Make backup of this file first before altering it
A_LoopFileNameNoExt := A_LoopFileNameNoExt()
;backup_filename := IncludeFolder "\" A_LoopFileDir "\" A_LoopFileNameNoExt "_backup." A_LoopFileExt
backup_filename := A_LoopFileDir "\" A_LoopFileNameNoExt "_backup." A_LoopFileExt
;msgbox % backup_filename
FileCopy, %A_LoopFileFullPath%, %backup_filename%, 1
; [ Insert a "Return" at end of auto-execute section ]
; Locate end of auto-execute section
Gosub Locate_End_Auto_Execute
; Make room for my "Return" line
;msgbox end_of_auto_execute = %end_of_auto_execute%
InsertBlankArrayLine2("FileLines", end_of_auto_execute, 4)
; Write my "Return" line (to array)
Return_line := end_of_auto_execute + 1
;msgbox Return_line = %Return_line%
FileLines%Return_line% = Return ;This is the end of the auto-execute section
; Now write back to file
FileLines := FileLines0
file_variable =
Loop, %FileLines%
{
line := FileLines%A_Index%
file_variable .= line . "`n"
}
;;msgbox about to save
FileDelete, %A_LoopFileFullPath%
FileAppend, %file_variable%, %A_LoopFileFullPath%
; Create a Gosub/Label/Return for this include file
include_file_count++
label = Include%include_file_count%
;;Gosubs_text := Gosubs_text%inc_folder_count%
Gosubs_text =
(ltrim
%Gosubs_text%
GoSub %label% ; Run the Auto-Execute code for this file
)
;;Labels_text := Labels_text%inc_folder_count% ;Labels_Includes_Contents_text
Labels_text =
(ltrim
%Labels_text%
%label%:
)
; Add #Include or <actual file contents>
;;Labels_text := Labels_text%inc_folder_count% ;Labels_Includes_Contents_text
;
If IncludeOrCombine = COMBINE
{ FileRead FullFile, %A_LoopFileFullPath%
Labels_text = ;actual content of file ;Labels_Includes_Contents_text
(ltrim
%Labels_text%
; #############################################################################
; ###################################### Beginning of file %A_LoopFileFullPath%
; #############################################################################
%FullFile%
; #############################################################################
; ###################################### End Of File %A_LoopFileFullPath%
; #############################################################################
)
}
Else
{ ; IncludeOrCombine = INCLUDE
Labels_text =
(ltrim
%Labels_text%
%A_Space%#Include %A_LoopFileName%
)
}
; Add trailing Return
;;Labels_text := Labels_text%inc_folder_count% ;Labels_Includes_Contents_text
Labels_text =
(ltrim
%Labels_text%
Return
)
Gosubs_text%inc_folder_count% := Gosubs_text
Labels_text%inc_folder_count% := Labels_text ;Labels_Includes_Contents_text
}
}
; Delete temp file
FileDelete %TempFileName%
; Create our new file:
; Create Top_text
Top_text =
(ltrim
;*** This script was automatically created from all the scripts found in the IncludeFolder(s) ***
#SingleInstance Force
)
; Create/combine Gosubs_text & Include_folder_Labels_text
inc_folders := inc_folder_count
Gosubs_text := "" , Include_folder_text := "" , Labels_text := ""
;Include_folder_Labels_text := ""
Loop, %inc_folders%
{
Gosubs_text .= Gosubs_text%A_Index% ; . "`n"
;
Include_folder_text := Include_folder_text%A_Index%
Labels_text := Labels_text%A_Index%
Include_folder_Labels_text .= Include_folder_text . Labels_text . "`n"
If (inc_folders>1 and A_Index<inc_folders)
Include_folder_Labels_text .= "`;`n"
}
; Create Your_Code_text
Your_Code_text =
(ltrim
;PUT YOUR CODE HERE (optional)
)
; Create Return_text
Return_text =
(ltrim
Return
)
; Create/combine all text into one variable --> Write to file
main_file_text := Top_text . Gosubs_text . Your_Code_text . Return_text . Include_folder_Labels_text
FileAppend, %main_file_text%, %TempFileName%
;>fe
; Open our new file
Run %TempFileName%
;>mainEnd
ExitApp
; Subroutines
Locate_End_Auto_Execute:
; Search for any/1st "::" hotkey (ie for end of A-E section)
FileRead, FileContents, %A_LoopFileFullPath%
;;msgbox reading %A_LoopFileFullPath%
;;msgbox contents = %FileContents%
StringSplit, FileLines, FileContents, `n, `r
;
end_of_auto_execute = 0
;;msgbox in here
;;msgbox FileLines0 = %FileLines0%
Loop, %FileLines0%
{
file_line := FileLines%A_Index%
;;msgbox file_line = %file_line%
If (pos:=InStr(file_line,"Return") > 0) ;DOESN'T handle commented :: (after ;)
{
; Check that not PART of another word
;
char_after := SubStr(file_line,pos+6,1)
If ( char_after=" " or char_after=A_Tab or char_after="" ) ;after
{
If (pos = 1) ;before
{
;end_of_auto_execute := A_Index (keep at 0 - so DOESN'T write "Return")
Break
}
Else
{
char_before := SubStr(file_line,pos-1,1)
If ( char_before=" " or char_before=%A_Tab% )
{
;end_of_auto_execute := A_Index (keep at 0 - so DOESN'T write "Return")
Break
}
}
}
}
IfInString, file_line, :: ;DOESN'T handle commented :: (after ;)
{
;;msgbox here -- %A_LoopFileFullPath%
end_of_auto_execute := A_Index
Break
}
IfInString, file_line, #IfWin ;DOESN'T handle commented #IfWin (after ;)
{
end_of_auto_execute := A_Index
Break
}
}
Return
/*
Edit:
FileSelectFolder, sel_folder_path, %MainFolder%
Return
*/
Build:
If (sel_set_file_path = "")
{
MsgBox,,, No Set File selected!, 2
return
}
Else
{
; @IncludeFolders:
; A list of all the folders to search:
; Read include folders data from selected Set File
FileRead, file_data, %sel_set_file_path%
StringSplit, file_data_array, file_data, `n, `r
; Add (valid) include folders to array (& Ignore/rid blank lines)
IncludeFolders =
raw_count := file_data_array0
Loop, %raw_count%
{
line := file_data_array%A_Index%
If ( Right(line,1) = "\" )
StringTrimRight, line, line, 1 ;Rid trailing "\" (normalize)
If (line <> "")
IncludeFolders .= line . "`n"
}
StringTrimRight, IncludeFolders, IncludeFolders, 1 ;rid trailing `n
build_it = 1
;;msgbox %IncludeFolders%
;;exitapp
}
Return
New:
Return
Open:
FileSelectFile, sel_set_file_path,, %MainFolder%,, *.txt
Return
Save:
Return
SaveAs:
Return
Quit:
Return
; Functions
A_LoopFileNameNoExt()
{
;MUST be within a "Loop, FilePattern" loop
;In main script: A_LoopFileNameNoExt := A_LoopFileNameNoExt()
len_ext := StrLen(A_LoopFileExt) + 1 ;1 = "."
StringTrimRight, A_LoopFileNameNoExt, A_LoopFileName, %len_ext%
return A_LoopFileNameNoExt
}
InsertBlankArrayLine2(arrayName, at_element, blank_elements)
{
Global ;So can access the array
array_elements := %arrayName%0
to_element := array_elements + blank_elements
from_element := array_elements
Loop
{
%arrayName%%to_element% := %arrayName%%from_element%
If (from_element = at_element)
{
; "Blank" the necessary lines
blank_this_element := at_element ;=from_element
Loop, %blank_elements%
{
%arrayName%%blank_this_element% =
blank_this_element++
}
Break ;Done all that NEED to do (can LEAVE all of array below this point)
}
to_element--
from_element--
}
; Update element count (re-write to array)
%arrayName%0 := array_elements + blank_elements
}
Right(String, Count=0)
{
StringRight, result, String, %Count%
return result
}
Ps: I haven't had any time to work on this in the past couple of weeks, but hopefully i'll get some time in a week or so
Edit: Added my 3 functions to the source