Esempio n. 1
0
        // Apply build number, version number, user name, and PC name to the source code
        public static void NormalizeSourceCode(int buildNumber, int version, string userName, string pcName, DateTime date)
        {
            DateTime now = date;

            char[] seps = { '\t', ' ', };

            int i = pcName.IndexOf(".");

            if (i != -1)
            {
                pcName = pcName.Substring(0, i);
            }

            userName = userName.ToLower();
            pcName   = pcName.ToLower();

            string[] files = Util.CombineArray <string>(
                Directory.GetFiles(Paths.BaseDirName, "*.h", SearchOption.AllDirectories));

            foreach (string file in files)
            {
                string dir = Path.GetDirectoryName(file);
                if (Str.InStr(dir, @"\.svn\") == false &&
                    Str.InStr(IO.GetRelativeFileName(file, Paths.BaseDirName), @"tmp\") == false)
                {
                    byte[] srcData = File.ReadAllBytes(file);

                    int      bomSize;
                    Encoding enc = Str.GetEncoding(srcData, out bomSize);
                    if (enc == null)
                    {
                        enc = Str.Utf8Encoding;
                    }
                    StringReader r = new StringReader(enc.GetString(Util.ExtractByteArray(srcData, bomSize, srcData.Length - bomSize)));
                    StringWriter w = new StringWriter();
                    bool         somethingChanged = false;

                    while (true)
                    {
                        string line = r.ReadLine();
                        if (line == null)
                        {
                            break;
                        }
                        string newLine = null;

                        string[] tokens = line.Split(seps, StringSplitOptions.RemoveEmptyEntries);

                        if (tokens.Length >= 1)
                        {
                            if (file.EndsWith(".h", StringComparison.InvariantCultureIgnoreCase))
                            {
                                if (tokens.Length == 3)
                                {
                                    // Build number portion of the source code
                                    if (tokens[0].Equals("//") && tokens[1].Equals("Build") && Str.IsNumber(tokens[2]))
                                    {
                                        newLine = line.Replace(tokens[2], buildNumber.ToString());
                                    }
                                }
                            }

                            if (file.EndsWith(".h", StringComparison.InvariantCultureIgnoreCase))
                            {
                                if (tokens.Length == 3)
                                {
                                    // String part of the version information of Cedar.h
                                    if (tokens[0].Equals("#define") && tokens[1].Equals("CEDAR_BUILD"))
                                    {
                                        newLine = line.Replace(tokens[2], buildNumber.ToString());
                                    }

                                    if (tokens[0].Equals("#define") && tokens[1].Equals("CEDAR_VER"))
                                    {
                                        newLine = line.Replace(tokens[2], version.ToString());
                                    }

                                    if (tokens[0].Equals("#define") && tokens[1].Equals("BUILDER_NAME"))
                                    {
                                        newLine = line.Replace(tokens[2], "\"" + userName + "\"");
                                    }

                                    if (tokens[0].Equals("#define") && tokens[1].Equals("BUILD_PLACE"))
                                    {
                                        newLine = line.Replace(tokens[2], "\"" + pcName + "\"");
                                    }

                                    if (tokens[0].Equals("#define") && tokens[1].Equals("BUILD_DATE_Y"))
                                    {
                                        newLine = line.Replace(tokens[2], date.Year.ToString());
                                    }

                                    if (tokens[0].Equals("#define") && tokens[1].Equals("BUILD_DATE_M"))
                                    {
                                        newLine = line.Replace(tokens[2], date.Month.ToString());
                                    }

                                    if (tokens[0].Equals("#define") && tokens[1].Equals("BUILD_DATE_D"))
                                    {
                                        newLine = line.Replace(tokens[2], date.Day.ToString());
                                    }

                                    if (tokens[0].Equals("#define") && tokens[1].Equals("BUILD_DATE_HO"))
                                    {
                                        newLine = line.Replace(tokens[2], date.Hour.ToString());
                                    }

                                    if (tokens[0].Equals("#define") && tokens[1].Equals("BUILD_DATE_MI"))
                                    {
                                        newLine = line.Replace(tokens[2], date.Minute.ToString());
                                    }

                                    if (tokens[0].Equals("#define") && tokens[1].Equals("BUILD_DATE_SE"))
                                    {
                                        newLine = line.Replace(tokens[2], date.Second.ToString());
                                    }
                                }

                                if (tokens.Length >= 3)
                                {
                                    if (tokens[0].Equals("#define") && tokens[1].Equals("SUPPORTED_WINDOWS_LIST"))
                                    {
                                        newLine = "#define\tSUPPORTED_WINDOWS_LIST\t\t\"" + OSList.Windows.OSSimpleList + "\"";
                                    }
                                }
                            }
                        }

                        if (newLine == null || newLine == line)
                        {
                            w.WriteLine(line);
                        }
                        else
                        {
                            w.WriteLine(newLine);

                            somethingChanged = true;
                        }
                    }

                    if (somethingChanged)
                    {
                        byte[] retData = Str.ConvertEncoding(Str.Utf8Encoding.GetBytes(w.ToString()), enc, bomSize != 0);

                        File.WriteAllBytes(file, retData);

                        Con.WriteLine("Modified: '{0}'.", file);
                    }
                }
            }
        }