Skip to content

Instantly share code, notes, and snippets.

@davebrny

davebrny/auto_include.ahk

Last active Jul 8, 2020
Embed
What would you like to do?
📃 (autohotkey) - create a list of .ahk files to be #included in your main script
/*
[search folders]
folder_1 =
folder_2 =
[ignore paths]
C:\Users\documents\scripts\example script name.ahk
C:\Users\documents\scripts\example folder name
[settings]
disable_warning = true
execute_names = \auto-executes\,\auto executes\
function_names = \functions\,\lib\
save_folder =
[stats]
script count =
list time =
*/
auto_include() {
start_time := a_tickCount
iniRead, disable_warning, % a_lineFile, settings, disable_warning
i := (disable_warning = "true") ? ("*i ") : ("")
iniRead, ignore_list, % a_lineFile, ignore paths
iniRead, execute_names, % a_lineFile, settings, execute_names
iniRead, function_names, % a_lineFile, settings, function_names
iniRead, save_folder, % a_lineFile, settings, save_folder
loop,
{
iniRead, search_folder, % a_lineFile, search folders, folder_%a_index%
if (search_folder = "")
continue
loop, files, % rTrim(search_folder, "\") . "\*.ahk", FR
{
if inStr(ignore_list . "`n", a_loopFileDir . "`n")
ignore_folder := a_loopFileDir
if a_loopFileDir contains %ignore_folder%
continue ; ignore certain folders
else if inStr(ignore_list, a_loopFileFullPath)
continue ; ignore certain files
script_count++
new_line := (a_loopFileDir = last_dir) ? "" : "`n"
last_dir := a_loopFileDir ; (add an extra line between folders)
if a_loopFileFullPath contains %execute_names%
executes .= new_line . "#include, " . i . a_loopFileFullPath . "`n"
else scripts .= new_line . "#include, " . i . a_loopFileFullPath . "`n"
if a_loopFileFullPath contains %function_names%
functions .= new_line . "#include, " . i . a_loopFileFullPath . "`n"
}
}
until (search_folder = "ERROR") ; loop end
if (save_folder = "")
save_folder := a_scriptDir "\include lists"
update_include_list(executes, save_folder "\~executes.ahk")
update_include_list(scripts, save_folder "\~scripts.ahk")
if (function_names)
update_include_list(functions, save_folder "\~functions.ahk")
; iniWrite, % " " script_count, % a_lineFile, stats, script count
; iniWrite, % " " (a_tickCount - start_time) " ms", % a_lineFile, stats, list time
}
update_include_list(new_text, list_file) {
file := fileOpen(list_file, "r `n")
current_text := file.read()
top_text := " `; ! auto-generated file. any changes made here will be overwritten`n"
new_text := strReplace(top_text . new_text, a_scriptDir, "%a_scriptDir%")
if (current_text != new_text)
{
if !fileExist(list_file)
{
splitPath, list_file, , file_dir
fileCreateDir, % file_dir
}
file := fileOpen(list_file, "w `n")
file.write(new_text)
}
file.close()
}
/*
[script info]
version = 1.10
description = create a list of .ahk files to be #included in your main script
author = davebrny
source = https://gist.github.com/davebrny/55de3ab40499e33e40324ac6a96b70c0
*/
@davebrny

This comment has been minimized.

Copy link
Owner Author

@davebrny davebrny commented Oct 6, 2017

auto_include()

usage:

  • add your script folders the search folders section at the top of the file

  • specify a save folder where you want the lists to be saved

  • #include auto_include.ahk into your main script or add it to one of the function libraries

  • put auto_include() in the auto-execute section of your main script

  • once the function has run and the lists are created, include ~executes.ahk into the auto-execute section and ~scripts.ahk anywhere else in your script, then reload

#include, %a_scriptDir%\include lists\~executes.ahk  
#include, %a_scriptDir%\include lists\~scripts.ahk  

 

more details:

- any .ahk file that is in a folder or sub-folder that matches the names in execute_names will be added to ~executes.ahk
- every other .ahk file (including functions) will be added to ~scripts.ahk

- an optional ~functions.ahk file is created that can be included into other scripts if needed. to stop this file from being created just clear out the names in the function_names

- to scan multiple folders, increment the number for each key name: folder_2, folder_3, folder_4

- to stop a certain file or folder from being scanned, add the path to ignore paths section

- if the save_folder is left empty then the lists will be created in %a_scriptDir%\include lists

- *i is added to the include command by default so you wont get warnings when you rename or move a file.
to remove this and turn the warnings back on, set disable_warning to false

@davebrny

This comment has been minimized.

Copy link
Owner Author

@davebrny davebrny commented Nov 4, 2017

include_folder()

this is basic version of the above auto_include() function that is designed to be more compact so it can be included in a self contained script

  • put include_folder() in the auto-execute section
include_folder(a_scriptDir)  
include_folder(a_scriptDir "\or a certain folder")  
  • include the following snippet into your script
include_folder(folder) {
    loop, files, % folder "\*.ahk", FR
        {
        if a_loopFileFullPath contains %a_scriptFullPath%,%a_lineFile%,\lib\
            continue
        stringReplace, loop_path, a_loopFileFullPath,  %a_scriptDir%, `%a_scriptDir`%
        new_includes .= (new_includes ? "`n" : "") . "#include, *i " . loop_path
        }

    file := fileOpen(a_scriptFullPath, "r `n")
    this_file := file.read()

    stringGetPos, pos, this_file, % "`;include_" "these:", R1
    stringMid, current_includes, this_file, pos + 17
    stringMid, text_before, this_file, pos-1, , L
    text_before := inStr(this_file, "`;include_" "these:") ? text_before : this_file

    if (current_includes != new_includes)
        {
        file := fileOpen(a_scriptFullPath, "w `n")
        file.write(text_before "`n`;include_" "these:`n" new_includes)
        }
    file.close()
}
  • ! dont put anything after ;include_these: or after the include files that will appear at the end of your script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.