If you are a one man development team, you probably don’t really have the need for a full blown version control system, yet creating source code backups for each released version is undoubtedly important.
如果您是一个人的开发团队,则可能实际上并不需要完整的版本控制系统,但是为每个发行版本创建源代码备份无疑是重要的。
By leveraging the power of post-build events and a simple batch script, you can easily add the ability to have Visual Studio automatically create a source code backup for each release code build.
通过利用生成后事件和简单的批处理脚本的功能,您可以轻松地添加功能,使Visual Studio为每个发布代码生成自动创建源代码备份。
怎么运行的 (How it works)
Our solution is simple: whenever a successful build event occurs, we have a batch script run which creates a compressed archive (optionally tagged and timestamped) of all files in the respective Visual Studio project folder.
我们的解决方案很简单:每当成功的构建事件发生时,我们就会运行一个批处理脚本,该脚本会为相应Visual Studio项目文件夹中的所有文件创建一个压缩归档文件(可以选择加标签并加盖时间戳)。
That’s it. All you have to do is follow the steps below.
而已。 您所要做的就是按照以下步骤操作。
设置自动构建备份 (Setting up automatic build backups)
First you will need to download and extract the batch script file from the link at the bottom of the article. Additionally, you will need the 7-Zip command line tool (this is included with a the ‘full’ version of the Project Build Backup script, or you can download it separately). In our example, we extracted these files to the directory “C:\Tools”, but any location will work.
首先,您需要从文章底部的链接下载并提取批处理脚本文件。 此外,您将需要7-Zip命令行工具(Project Build Backup脚本的“完整”版本附带此工具,或者您可以单独下载)。 在我们的示例中,我们将这些文件提取到目录“ C:\ Tools”,但是任何位置都可以使用。
Open your Visual Studio Project properties, by double-clicking on My Project under the respective project.
通过双击相应项目下的我的项目,打开Visual Studio项目属性。
In the project properties, go to the Compile section.
在项目属性中,转到“编译”部分。
In the bottom right corner, click the Build Events button.
在右下角,单击“构建事件”按钮。
In our case, we want to make a backup after a successful compile action. Make sure you have the option to run the post-build event “On successful build” and then click the Edit Post-build button.
就我们而言,我们希望在成功执行编译操作后进行备份。 确保您可以选择运行构建后事件“成功构建”,然后单击“编辑构建后”按钮。
The command below creates a build backup only for the compile of the Release configuration (this is what IF condition checks for) as, realistically, we probably don’t want to make a backup of each Debug/testing build. Additionally, the current timestamp will be appended (/D switch) with the backup file being in 7z file format (/7z) as opposed to zip. By adding the /T “$(ConfigurationName)” as a parameter, we are appending the build type (Release in this case) to the name of the backup file.
下面的命令仅针对Release配置的编译(这是IF条件检查的内容)创建构建备份,因为实际上,我们可能不想为每个Debug / test构建进行备份。 此外,当前时间戳将附加(/ D开关),备份文件采用7z文件格式(/ 7z),而不是zip。 通过添加/ T“ $(ConfigurationName)”作为参数,我们将构建类型(在本例中为Release)附加到备份文件的名称。
IF “$(ConfigurationName)” == “Release” CALL C:\Tools\ProjectBuildBackup.bat “$(SolutionDir)” “$(ProjectDir)” “$(ProjectName)” /T “$(ConfigurationName)” /D /7z
IF“ $(ConfigurationName)” ==“释放” CALL C:\ Tools \ ProjectBuildBackup.bat“ $(SolutionDir)”“ $(ProjectDir)”“ $(ProjectName)” / T“ $(ConfigurationName)” / D / 7z
Using the Macros button, you can have Visual Studio prefill project specific information so no hardcoding is required. You can adjust this command as needed (especially the location of the batch file), but the first three parameters will likely not need to be changed.
使用“宏”按钮,可以使Visual Studio预填充项目特定的信息,因此不需要进行硬编码。 您可以根据需要调整此命令(尤其是批处理文件的位置),但是前三个参数可能不需要更改。
It is important to keep in mind that post-event operations run regardless of the project configuration selected. This is why we need to add the IF “$(ConfigurationName)” == “Release” statement – otherwise the backup action would occur on every successful build event.
重要的是要记住,事件后操作无论选择的项目配置如何都将运行。 这就是为什么我们需要添加IF“ $(ConfigurationName)” ==“ Release”语句的原因–否则,备份操作将在每个成功的构建事件上发生。
Once you finish your command and apply it, the command string should appear in the Post-build events section.
完成命令并应用后,命令字符串应出现在“构建后事件”部分中。
Note that while the “CALL” command is not technically required, it is highly recommended, as if this is omitted then any events added after this may not execute.
请注意,尽管从技术上讲不是“ CALL”命令,但强烈建议您这样做,因为好像省略了该命令,则此后添加的任何事件都可能不会执行。
Now whenever you run a compile/build with your project in the Release configuration, you will see the output from the build backup operation.
现在,无论何时在Release配置中对项目运行编译/构建,您都将看到构建备份操作的输出。
[…]
[…]
Each successful Release build creates a new timestamped archive with the solution folder in a subdirectory, “Builds” (which can be custom defined with the /O switch if needed).
每个成功的Release构建都将创建一个带有时间戳的新存档,并将解决方案文件夹放在子目录“ Builds”(可以根据需要使用/ O开关进行自定义)。
The contents of each backup is the full Visual Studio project – source files, configuration settings, compiled binaries, and all – which makes this a true point in time backup.
每个备份的内容都是完整的Visual Studio项目-源文件,配置设置,已编译的二进制文件以及所有文件-这使这成为真正的时间点备份。
不能代替完整版本控制系统 (Not a replacement for a full version control system)
In closing, we just want to reiterate that this tool is not intended to replace a full blown version control system. It is simply a useful tool for developers to create snapshots of their project’s source code after each compilation.
最后,我们只想重申一下,该工具无意替代完整版本控制系统。 它是开发人员在每次编译后创建其项目源代码快照的简单有用的工具。
In the event you ever have to go back and examine a prior version, having a ready-to-use (just extract to a new directory) project file for a point in time compilation can really come in handy.
如果您不得不回过头来检查以前的版本,那么拥有一个随时可用(只需提取到新目录)项目文件以进行时间点编译就可以派上用场。
链接 (Links)
Download Project Build Backup Script
Download 7-Zip Command Line Tool (Note – the 7za utility is also bundled with a download from the Project Build Backup script)
下载7-Zip命令行工具 (注意– 7za实用程序还与从Project Build Backup脚本中下载的内容捆绑在一起)
翻译自: https://www.howtogeek.com/167283/how-to-automatically-create-build-backups-in-visual-studio/
5090

被折叠的 条评论
为什么被折叠?



