如何使用NAnt更新VS2005 Web部署项目生成的dll版本号

时间:2022-08-26 07:21:59

I have a NAnt script that compiles our web application, but would like to programatically update the version number of the assembly it generates. I have sourced the version numbers already and you can assume they have been stored in some NAnt variables.

我有一个编译我们的Web应用程序的NAnt脚本,但是想以编程方式更新它生成的程序集的版本号。我已经获得了版本号,您可以假设它们已经存储在一些NAnt变量中。

Can figure out how to do this for standard projects, but not for web deployment projects.

可以弄清楚如何为标准项目执行此操作,但不能为Web部署项目执行此操作。

Any help you can provide would be appreciated.

您可以提供的任何帮助将不胜感激。

Thanks in advance!

提前致谢!

1 个解决方案

#1


You have to write the version number into the web deployment project file itself. This NAnt task should do it:

您必须将版本号写入Web部署项目文件本身。这个NAnt任务应该这样做:

  <target name="setAssemblyVersion" description="Increments/Sets the AssemblyVersion value" depends="getAssemblyVersion">
    <foreach item="File" property="filename">
      <in>
        <items>
          <include name="**/*.wdproj"></include>
        </items>
      </in>
      <do>
        <script language="C#">
          <code>
            <![CDATA[
           public static void ScriptMain(Project project) {
               string contents = "";
               StreamReader reader = new StreamReader(project.Properties["filename"]);
               contents = reader.ReadToEnd();
               reader.Close();
               string replacement = string.Format(
                   "<Value>{0}.{1}.{2}.{3}</Value>",
                   project.Properties["build.major"],
                   project.Properties["build.minor"],
                   project.Properties["build.build"],
                   project.Properties["svn.revision"]
               );  

               string newText = Regex.Replace(contents, @"<Value>([0-9]+\.){3}[0-9]+</Value>", replacement);
               StreamWriter writer = new StreamWriter(project.Properties["filename"], false, Encoding.UTF8);
               writer.Write(newText);
               writer.Close();
           }        
           ]]>
          </code>
        </script>
      </do>
    </foreach>
  </target>

I'm guessing your NAnt property names...

我猜你的NAnt财产名称......

#1


You have to write the version number into the web deployment project file itself. This NAnt task should do it:

您必须将版本号写入Web部署项目文件本身。这个NAnt任务应该这样做:

  <target name="setAssemblyVersion" description="Increments/Sets the AssemblyVersion value" depends="getAssemblyVersion">
    <foreach item="File" property="filename">
      <in>
        <items>
          <include name="**/*.wdproj"></include>
        </items>
      </in>
      <do>
        <script language="C#">
          <code>
            <![CDATA[
           public static void ScriptMain(Project project) {
               string contents = "";
               StreamReader reader = new StreamReader(project.Properties["filename"]);
               contents = reader.ReadToEnd();
               reader.Close();
               string replacement = string.Format(
                   "<Value>{0}.{1}.{2}.{3}</Value>",
                   project.Properties["build.major"],
                   project.Properties["build.minor"],
                   project.Properties["build.build"],
                   project.Properties["svn.revision"]
               );  

               string newText = Regex.Replace(contents, @"<Value>([0-9]+\.){3}[0-9]+</Value>", replacement);
               StreamWriter writer = new StreamWriter(project.Properties["filename"], false, Encoding.UTF8);
               writer.Write(newText);
               writer.Close();
           }        
           ]]>
          </code>
        </script>
      </do>
    </foreach>
  </target>

I'm guessing your NAnt property names...

我猜你的NAnt财产名称......