首页 > 其他分享 >[AHK2] 合并使用#include的脚本

[AHK2] 合并使用#include的脚本

时间:2023-10-22 18:22:54浏览次数:33  
标签:脚本 buildin copy Join AHK2 Path path line include

这个脚本用于将一个脚本中的#include语句包含的脚本添加到这条#include语句的位置。
同时,它有其他功能,如:去除空行、注释(仅单行)、替换内置变量。
因为脚本原理是读取单行并处理,所以只能处理单行注释,要做更多复杂功能就需要使用其他方法了,比如索引表……
但脚本主要目的就是合并分部分的脚本,方便编译成exe。

; ========
; by wtdkwd 2023/10/21
; ========
; include files see `https://gitee.com/dkwd/ahk.git`
#Requires AutoHotkey v2.0
#SingleInstance Force

#Include 'G:\AHK\gitee_ahk2\common\Extend.ahk'
#Include 'G:\AHK\gitee_ahk2\common\Path.ahk'

require := Map()                              ; store the require statements
directives := Map()
included := Map()

buildin := Map()

requiresRE := 'i)#requires\s(.*)'
includedRE := 'i)#include\s[`'|"](.*)[`'|"]'
commentRE := '\s*;.*$'                        ; This RE only handles single-line comments
trailingComment := '^(.*?)\s+;.*?$'           ; trailingComment

RemoveComment(&line) {                        ; Remove the comment
  copy := line
  if copy ~= commentRE {
    copy := ''
  } else if RegExMatch(copy, trailingComment, &match) {
    copy := match[1]
  } else {
    copy := copy
  }
  line := copy
}

ReplaceKeyWord(&input) {                      ; Remove the buildin keyworlds
  copy := input
  for k, v in buildin
    copy := StrReplace(copy, k, v)
  input := copy
}

_Trim(&input) {                               ; Remove before and after Spaces
  copy := input
  input := Trim(copy)
}

filters := [_Trim, RemoveComment, ReplaceKeyWord]

_Trim_(singleLine) {
  for fn in filters {                         ; Run filter
    fn(&singleLine)
  }
  return singleLine ? singleLine '`r`n' : ''
}

ConcatPath(curr, _path) {
  Path.Normalize(&curr)
  Path.Normalize(&_path)
  segs := []

  curr := StrSplit(curr, Path.Sep)
  _path := StrSplit(_path, Path.Sep)

  loop _path.Length {
    if '..' == _path[A_Index] {
      curr.Pop()
    } else if '.' != _path[A_Index] && '' != _path[A_Index] {
      segs.Push(_path[A_Index])
    }
  }
  return curr.Concat(segs).Join(Path.Sep)
}

Reset() {
  global
  require.Clear()
  directives.Clear()
  included.Clear()
}

Resolve(fileFullPath) {
  resolved := ''
  global currentFile := fileFullPath
  currentDir := Path.ParseFilePath(fileFullPath).dir
  f := FileOpen(fileFullPath, 'r', 'utf-8')
  while !f.AtEOF {
    oriLine := f.ReadLine()
    if !oriLine                               ; brank line
      continue
    line := _Trim_(oriLine)
    d.Join line, oriLine
    if !line                                  ; brank line
      continue
    if line ~= '^#' {
      if line ~= requiresRE {
        RegExMatch(line, requiresRE, &match)
        content := match[1]
        if require.Has(content)
          line := ''
        require.set(fileFullPath, content)
      } else if line ~= includedRE {
        RegExMatch(line, includedRE, &match)
        includeFilePath := match[1]
        if Path.IsAbsolute(includeFilePath) { ; If it is an absolute path, no processing is needed
          resolvedPath := includeFilePath
        } else {                              ; Get the correct absolute path
          resolvedPath := ConcatPath(currentDir, includeFilePath)
        }
        if not included.Has(resolvedPath) {
          included.Set(resolvedPath, true)
          resolved .= Resolve(resolvedPath)   ; Recursive processing
        }
        line := ''
      } else {
        directives.Set(fileFullPath, line)
      }
    }
    resolved .= line
  }
  return resolved
}

#Include 'G:\AHK\gitee_ahk2\common\debug.ahk'
d := Debug('Merge.ahk - A tool for merging partial ahk scripts  -- Drag the file you want to merge into the gui', 1600, 800, , , , 'DETAIL', false)

d.Log()
d.OnEvent('DropFiles', OnDropFiles)

OnDropFiles(GuiObj, GuiCtrlObj, FileArray, X, Y) {
  d.ClearContent()
  Reset()
  DroppedFile := FileArray[1]
  parsed := Path.ParseFilePath(DroppedFile)
  output := parsed.dir Path.Sep parsed.fileName '_Merged.' parsed.ext       ; the output path
  global currentFile := DroppedFile
  global buildin
  buildin.Set('A_LineFile', "'" currentFile "'")                            ; keywords
  prefix := A_Space.repeat(14)
  d.Join 'filter count: '
  d.Join prefix filters.Length
  d.Join 'filter funcs: ', 'function name'
  for v in filters
    d.Join prefix v.Name
  d.Join 'buildin info: ', 'The keyword to replace'
  for k, v in buildin
    d.Join prefix k
  d.Divi 'RESOLVE'
  result := Resolve(DroppedFile)
  d.Divi('RESULT', , true)
  subStrings := StrSplit(result, '`r`n')
  for v in subStrings
    d.Join v, '-'
  d.Divi('REQUIRES')
  for k, v in require
    d.Join v, k
  d.Divi('DIRECTIVES')
  for k, v in directives
    d.Join v, k
  d.Divi('INCLUDE')
  for k, v in included
    d.Join k
  d.Log()

  f := FileOpen(output, 'w', 'utf-8')
  f.Write(result)
  f.Close()

  MsgBox 'The merged files have been saved to: ' output
}

标签:脚本,buildin,copy,Join,AHK2,Path,path,line,include
From: https://www.cnblogs.com/refiz/p/17780795.html

相关文章

  • [AHK2] 向对象原型添加属性和方法
    ahk和js十分相似,其中一点就是可以向本地对象添加自定义方法和属性。下面的脚本向ahk的字符串,数组添加了许多方法,添加之后在使用上就和js更加相似了。;Thisscriptisusedtoextendthemethodsoftheahknativeobjectprototype#RequiresAutoHotkeyv2.0#SingleInstan......
  • 生产环境rman备份脚本
    概述 RMAN(RecoveryManager)是Oracle数据库的备份和恢复工具。它是Oracle提供的官方工具,专门用于管理数据库备份、还原和恢复操作。内容#!/bin/bash#source/home/orace/.bash_profileexportORACLE_SID=oa2exportDATE=`date+%F`exportBACK_DIR='/u01/oa_backup/'mkdi......
  • TCL脚本语言学习
    前言  TCL(ToolCommandLanguage)命令的格式是命令+字符串,第一个是命令,后面都是字符串,tcl的解释器(逐行执行)会根据命令去对后面的字符串进行相关操作。注释符号:#一、安装启动tcl命令行,以%开头sudoaptinstalltcl//安装tcltclsh//启动tcl%二、变量列表1、置换subtitutio......
  • 12、Linux中shell脚本
    Linux中shell脚本目录Linux中shell脚本一、基础知识1、第一个shell脚本程序2、shell变量定义3、shell变量的赋值、修改、删除4、shell特殊变量二、脚本使用1、静态IP修改-交互式脚本2、主机存活探测-if脚本3、主机存活探测-for脚本4、主机存活探测-while脚本5、纯净查杀-case脚本......
  • 中医知识科普短视频脚本
    中医知识科普短视频脚本标题:中医基本理论之五行学说(背景音乐开启,出现动画logo,标题出现)旁白:大家好,欢迎来到我们的医学科普短视频。今天,我们要讲解的是中医基本理论之五行学说。(出现五行元素的动画图像:木、火、土、金、水)旁白:五行理论,是中国古代哲学的重要组成部分,也是中医学......
  • shell脚本示例
    目录1.编写脚本技巧2.脚本:color3.脚本:for循环嵌套4.脚本:检测网址联通性5.脚本:密钥分发6.生产脚本:TCP连接数监控-统计TCP11种状态连接数7.生产脚本:日志监控-检查日志刷新时间8.生产脚本-mq队列监控9.脚本:处理ftp文件10.脚本:数组案例11.whilereadline判断两个文件夹相......
  • shell脚本自动化实战
    Shell脚本自动化部署实战(二)原创 叶凡Jonas 软件测试成长之路 2023-09-0100:00 发表于上海收录于合集#UI自动化系列54个三丶shell语法4.程序结构2.循环结构说明:在上一篇博客中讲到了for循环,现在开始讲解while循环a)格式while[条件]do 命令done示例1:变量......
  • 25 个超棒的 Python 脚本合集
     Python是一种功能强大且灵活的编程语言,拥有广泛的应用领域。下面是一个详细介绍25个超棒的Python脚本合集:1.网络爬虫:使用Python可以轻松编写网络爬虫,从网页中提取数据并保存为结构化的格式。2.数据清洗和预处理:Python提供了许多库和工具,用于数据清洗、去重、填充缺失值和......
  • python脚本中应用多线程和多进程理解
    脚本内容因为要读取mongo某个全表数据(亿级别),有个字段有索引且是一堆多的关系从其他表读取所有这个字段(十万级别),再读取大表因为数据量大所以写个测试,从中拿出几条去大表查询(每次读到十万级别数据)多线程和多进程的影响不使用多线/进程file=open('test2.csv','w')content......
  • Linux-shell脚本使用ssh远程执行命令通过密码的方式登录
    1. sshpass简介sshpass是一个在非交互式ssh会话中自动输入密码的工具。它可以直接在命令行中指定密码,因此可以用于Shell脚本等自动化场景。在RedHat系统中,可以通过epel-release源安装sshpass。epel-release源是ExtraPackagesforEnterpriseLinux(EPEL)的缩写......