PowerShell 脚本的后缀是 .ps1

前提:

ps1 脚本可以帮忙我们快速修改文件内容,还不需要调用文件的底层 api,方便快捷

在编写 CMakeLists 时发现,项目不能够很好的使用 vcpkg tool chain,哪怕是在命令行中指定 vcpkg.cmake

如果只是简单的项目,vcpkg tool chain 可以正常工作,但是在稍微复杂一些的项目中,比如依赖的 vcpkg 的库过多,就会发现在编译时提示找不到相关的库

不过总的来说,这些都不是本文的重点,重点是如何编写好一个 ps1 脚本

正文:

我们需要读取一个文件的内容,并修改文件中的某个变量名,以及在特定的上下文中插入自定义字符串

# 读取项目文件内容
$scriptDir = $PSScriptRoot
$vcxprojPath = Join-Path $scriptDir "\build\project.vcxproj"
$vcxprojContent = Get-Content $vcxprojPath # 定义常量
$configurationPlatformDebug = '"''$(Configuration)|$(Platform)''==''Debug|Win32''"'
$configurationPlatformRelease = '"''$(Configuration)|$(Platform)''==''Release|Win32''"' # 添加 Vcpkg
$targetLine1 = ' <GenerateManifest Condition=' + $configurationPlatformRelease + '>true</GenerateManifest>'
$targetLine2 = ' </PropertyGroup>' # 修改 Debug 配置
$targetLine3 = ' <OutDir Condition=' + $configurationPlatformDebug + '>' + (Join-Path $scriptDir "build\Debug\") + '</OutDir>'
$targetLine4 = ' <IntDir Condition=' + $configurationPlatformDebug + '>project.dir\Debug\</IntDir>'
$targetLine5 = ' <TargetName Condition=' + $configurationPlatformDebug + '>project</TargetName>' # 修改 Release 配置
$targetLine6 = ' <OutDir Condition=' + $configurationPlatformRelease + '>' + (Join-Path $scriptDir "build\Release\") + '</OutDir>'
$targetLine7 = ' <IntDir Condition=' + $configurationPlatformRelease + '>project.dir\Release\</IntDir>'
$targetLine8 = ' <TargetName Condition=' + $configurationPlatformRelease + '>project</TargetName>' $targetLine9 = ' <PropertyGroup Condition=' + $configurationPlatformRelease + ' Label="Configuration">' $targetLine10 = ' <TargetExt Condition=' + $configurationPlatformRelease + '>.exe</TargetExt>' # 要替换的新文本
$newAttributes = @"
<PropertyGroup Label="Vcpkg" Condition=$configurationPlatformDebug>
<VcpkgConfiguration>Debug</VcpkgConfiguration>
</PropertyGroup>
<PropertyGroup Label="Globals">
<VcpkgTriplet Condition="'`$(Platform)'=='Win32'">x86-windows-static</VcpkgTriplet>
</PropertyGroup>
"@ $newTargetLines3 = ' <OutDir Condition=' + $configurationPlatformDebug + '>$(SolutionDir)bin\$(Configuration)\</OutDir>'
$newTargetLines4 = ' <IntDir Condition=' + $configurationPlatformDebug + '>$(SolutionDir)temp\$(Configuration)\$(ProjectName)\</IntDir>'
$newTargetLines5 = ' <TargetName Condition=' + $configurationPlatformDebug + '>$(SolutionName)_d</TargetName>' $newTargetLines6 = ' <OutDir Condition=' + $configurationPlatformRelease + '>$(SolutionDir)bin\Project\$(PJVersion)\</OutDir>'
$newTargetLines7 = ' <IntDir Condition=' + $configurationPlatformRelease + '>$(SolutionDir)temp\$(Configuration)\$(ProjectName)\</IntDir>'
$newTargetLines8 = ' <TargetName Condition=' + $configurationPlatformRelease + '>$(SolutionName)</TargetName>' $newTargetLines9 = ' <ConfigurationType>DynamicLibrary</ConfigurationType>' $newTargetLines10 = ' <TargetExt Condition=' + $configurationPlatformRelease + '>.dll</TargetExt>' $foundLines = @() # 因为只遍历一遍,所以要按先后顺序放置待修改的行
for ($i = 0; $i -lt $vcxprojContent.Length - 1; $i++) {
$line = $vcxprojContent[$i] # 检查是否找到目标行
if ($line -eq $targetLine9) {
$foundLines += $i + 1
} if ($line -eq $targetLine3) {
$foundLines += $i
} if ($line -eq $targetLine4) {
$foundLines += $i
} if ($line -eq $targetLine5) {
$foundLines += $i
} if ($line -eq $targetLine6) {
$foundLines += $i
} if ($line -eq $targetLine7) {
$foundLines += $i
} if ($line -eq $targetLine8) {
$foundLines += $i
} if ($line -eq $targetLine10) {
$foundLines += $i
} if ($line -eq $targetLine1 -and $vcxprojContent[$i + 1] -eq $targetLine2) {
$foundLines += $i + 2
} } # 判断是否找到所有待替换的行
if ($foundLines.Count -eq 9) {
# 替换目标行
$vcxprojContent[$foundLines[0]] = $newTargetLines9
$vcxprojContent[$foundLines[1]] = $newTargetLines3
$vcxprojContent[$foundLines[2]] = $newTargetLines4
$vcxprojContent[$foundLines[3]] = $newTargetLines5
$vcxprojContent[$foundLines[4]] = $newTargetLines6
$vcxprojContent[$foundLines[5]] = $newTargetLines7
$vcxprojContent[$foundLines[6]] = $newTargetLines8
$vcxprojContent[$foundLines[7]] = $newTargetLines10
# 在目标行后面插入新文本
$vcxprojContent = [System.Collections.ArrayList]($vcxprojContent -split "`r`n")
$vcxprojContent.Insert($foundLines[8], $newAttributes)
# 将修改后的内容保存回文件
$vcxprojContent | ForEach-Object { $_ } | Set-Content $vcxprojPath Write-Host "Target lines replaced successfully in project."
} else {
Write-Host "Specific lines not found in the file in project."
}

我们还可以通过 ps1 脚本改文件编码格式

比如 visual studio 默认接受 UTF-16 编码的 rc 文件,而我们使用 CMake 中的 configure_file 函数生成的文件默认是 UTF-8 编码

那么我们可以使用 -Encoding 来改变

# project.rc 转为 UTF-16 编码
$rcPath = Join-Path $scriptDir "\build\project.rc"
# 读取 UTF-8 编码的文件内容
$content = Get-Content -Path $rcPath -Encoding UTF8
# 将内容以 UTF-16 编码保存
Set-Content -Path $rcPath -Value $content -Encoding Unicode

补充:

时间匆忙,无法对每个 ps1 的函数一一讲解,有兴趣的可以查阅文档来了解其作用

如何编写一个 PowerShell 脚本的更多相关文章

  1. 编写一个BAT脚本协助运维人员遇到问题时候调测数据库是否有效连接成功的操作攻略

    简单摘要: 1.内网系统出现故障需要排查 2.运维人员不熟悉数据库操作,没法通过连接数据库和执行SQL语句的方式排查数据库及数据是否正常 3.解决方案:编写一个bat脚本,运维人员双击运行即可.   ...

  2. 工程师技术(五):Shell脚本的编写及测试、重定向输出的应用、使用特殊变量、编写一个判断脚本、编写一个批量添加用户脚本

    一.Shell脚本的编写及测 目标: 本例要求两个简单的Shell脚本程序,任务目标如下: 1> 编写一个面世问候 /root/helloworld.sh 脚本,执行后显示出一段话“Hello ...

  3. shell编写一个判断脚本

                                                              shell编写一个判断脚本 4.1问题 本例要求在虚拟机server0上创建/roo ...

  4. 从0开始的Python学习013编写一个Python脚本

    通过之前的学习我们已经了解了Python的很多基础运用了,现在我们尝试着做一个有使用价值的小脚本. 问题 需求: 我想要一个可以给我备份重要文件的程序. 需求分析: 首先文件是有存储路径,文件的路径和 ...

  5. python+pytest接口自动化(12)-自动化用例编写思路 (使用pytest编写一个测试脚本)

    经过之前的学习铺垫,我们尝试着利用pytest框架编写一条接口自动化测试用例,来厘清接口自动化用例编写的思路. 我们在百度搜索天气查询,会出现如下图所示结果: 接下来,我们以该天气查询接口为例,编写接 ...

  6. 分享我编写的powershell脚本:ssh-copy-id.ps1

      问:通过[字符串界面].如何从win,通过ssh,连接到sshd?答:在任意版本win中,通过cmd.exe,powershell.exe中调用ssh.exe,连接sshd.   问:通过[pow ...

  7. 编写一个python脚本功能-备份

    版本一 解决方案当我们基本完成程序的设计,我们就可以编写代码了,它是对我们的解决方案的实施.版本一例10.1 备份脚本——版本一 #!/usr/bin/python # Filename: backu ...

  8. 1.编写一个shell脚本

    一.shell和shell脚本 在linux系统下,以 #/bin/bash开头的文本会被shell解释器进行解释.   shell是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操 ...

  9. sql server编写一个语句脚本自动清空各表数据以初始化数据库

    问题:有时已有项目要移植,例如原来在广州地区使用的某系统,突然说惠州那边也要用这套一样的系统.或者,在demo环境下弄了一些测试数据.然后要清空全部表数据.如果表比较多的话,逐个表手工编写脚本就太麻烦 ...

  10. Python编写一个Python脚本

    我想要一个可以为我的所有重要文件创建备份的程序.(下面测试环境为python2.7) 1.backup_ver1.py #!/usr/bin/python import os import time ...

随机推荐

  1. [转帖]@Scope("prototype")的正确用法——解决Bean的多例问题

    https://www.jianshu.com/p/54b0711a8ec8 1. 问题,Spring管理的某个Bean需要使用多例   在使用了Spring的web工程中,除非特殊情况,我们都会选择 ...

  2. [转帖]S3FS 简介及部署

    PS:文章一般都会先首发于我的个人Blog上:S3FS 简介及部署 · TonghuaRoot's BloG. ,有需要的小伙伴可以直接订阅我的Blog,获取最新内容. 0x00 前言 S3FS可以把 ...

  3. [转帖]Linux下的I/O复用与epoll详解

    https://blog.csdn.net/weixin_39094034/article/details/110393127 前言 I/O多路复用有很多种实现.在linux上,2.4内核前主要是se ...

  4. 【转帖】linux环境下使用route指令设置多个网络连接的优先级(通过修改路由表的默认网关条目)

    1. 背景 在生活中的会经常遇见一台PC同时连接多个网络的场景.最典型的,一台笔记本可以同时连接一个无线网(手机热点)和一个有线网(以太网).linux和window操作系统在默认情况都会使用最早连接 ...

  5. [转帖]Linux下使用 ipset 封大量IP及ipset参数说明

    https://www.cnblogs.com/xiaofeng666/p/10952627.html Linux使用iptables封IP,是常用的应对网络攻击的方法,但要封禁成千上万个IP,如果添 ...

  6. 【JS 逆向百例】元素ID定位加密位置,某麻将数据逆向

    声明 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关,若有侵权,请联系我立即删除! 逆向目标 目标:某在线麻将 ...

  7. linux如何配置ssh密钥登录

    为什么要用ssh密钥登录 购买的服务器设置密码很容易被暴力破解,用密钥登录安全很多.root用户新建的用户也要用密钥登录更安全,如果一直su - 用户名登录 不方便 用xftp等服务上传文件到用户使用 ...

  8. TienChin 活动管理-活动状态完善

    修改字典 修改活动状态字典,将之前的数据键值为 0 的数据标签内容改为 过期: 更改下数据库的描述,禁用改为过期: ALTER TABLE `tienchin_activity` MODIFY COL ...

  9. 【druid切换hikari连接池】通过源码分析遇到的问题

    一.前言说明 如果不会配置druid连接池的话,可以参考我这篇博文:springboot整合druid: springboot整合所有的starter方法基本都差不多,添加依赖,开启注解,编写配置,增 ...

  10. 因为命名被diss无数次。简单聊聊编程最头疼的事情之一:命名

    本文已经收录进我的 80K+ Star 的 Java 开源项目 JavaGuide:https://github.com/Snailclimb/JavaGuide (「Java学习+面试指南」一份涵盖 ...