Esempio n. 1
0
        static public void ValidateFile(string P)
        {
            FileCountLimit--;
            FileCount++;
            CkanTokeniser CT = new CkanTokeniser(P);

            CT.AllowSlashN = true;
            TokenFile Foo = new TokenFile(CT);

            Foo.parse();
            CKanFormat bar = new CKanFormat(Foo);

            bar.AllowEOLs = true;

            if (!bar.validation())
            {
                InValidCount++;
                Console.Error.WriteLine($"Error: Parsing Failure ({InValidCount}) in File: <{P}>");
                bar.validation(true);
                pause();
            }
        }
        // static string[] NameFields = { "\"spec_version\"", "\"identifier\"", "\"name\"", "\"author\"", "\"version\"",
        //     "\"ksp_version_min\"", "\"ksp_version_max\"", "\"license\"", "\"provides\"", "\"conflicts\"" };
        /// <summary>
        /// This function validates the localises one specified input ckan File and generates/overwrites one output ckanfile
        /// CkanLocaliser Localise  AbsPathToMod SrcFile.ckan  DestFile.ckan [options].
        /// </summary>
        static public int Localise(string[] args)
        {
            CkanLocaliserClass.DoingWhat.Push("Localise");

            // Validate command line Parameters.
            if (args.Length < 4)
            {
                Usagesmsg();
                return(-1);
            }
            if (true)
            {
                using (StreamWriter sw = File.CreateText(args[3]))
                {   // This makes sure that if we can to avoid confusion the outfile does in no sense look valid
                    sw.WriteLine(" { Localise error } ");
                    sw.Close();
                }
            }


            if (!args[1].EndsWith(".zip"))
            {
                Console.Error.WriteLine("ModFile MUSTR be a zip File");
                Usagesmsg();
                return(-1);
            }
            DownloadPath = args[1];
            if (DoAWhiteBoxtest)
            {   // This allows test code to skip having an actual binary zip file.
                DownloadSize = 9999;
                SHA1         = "F550FBBEF92224DD32E1E0D045BAA8C95EF45343";
                SHA256       = "D51D322B8F68FCDE25FCF4334327C0B86AAE0A65E9B2B70B1B03ECC7761AFD2C";
            }
            else
            {
                FileInfo fi1 = new FileInfo(DownloadPath);
                if (!fi1.Exists)
                {
                    Console.Error.WriteLine("ModFile Must exist");
                    Usagesmsg();
                    return(-1);
                }
                if (!Path.IsPathRooted(DownloadPath))
                {
                    throw new FormatException($"Fatal Error: DownLoad path must be an absolute rooted path \"{DownloadPath}\" is not.", true);
                }
                DownloadSize = fi1.Length;

                SHA1   = GetFileHashSha1(DownloadPath);
                SHA256 = GetFileHashSha256(DownloadPath);
            }

            FileInfo fi2 = new FileInfo(args[2]);

            if (!fi2.Exists)
            {
                Console.Error.WriteLine("SrcFile Must exist");
                Console.Error.WriteLine(" PWD is :" + Directory.GetCurrentDirectory());
                Usagesmsg();
                return(-1);
            }
            // Validate Ckan File

            CkanTokeniser CT = new CkanTokeniser(args[2]);
            // CT.AllowSlashN = true;
            TokenFile Foo = new TokenFile(CT);

            Foo.parse();
            CKanFormat bar = new CKanFormat(Foo);

            bar.AllowEOLs = true;

            if (!bar.validation())
            {
                string DL = "\n====================================\n";
                Console.Error.WriteLine($"\n\n{DL}### Validation Error: Parsing Failure in File: <{args[2]}> \n Context in which That happened {DL}");
                bar.validation(true);
                return(-1);
            }
            // do The localisation and write the file
            try
            {   // First tag all three common human readable Fields to identify That we are localised
                CkanLocaliserClass.DoingWhat.Push("RewritingFields");
                parseToValueFor(bar, "\"identifier\"");
                OldIdentifier = bar.Curs.TokenObj.theToken;                                                 // Preserve what it was Called We will need that later
                bar.Curs.TokenObj.theToken = "\"" + Prefix + OldIdentifier.Substring(1);                    // prepend the Localising prefix
                                                                                                            //                Console.WriteLine(Curs.TokenObj.theToken);
                parseToValueFor(bar, "\"name\"");
                bar.Curs.TokenObj.theToken = "\"" + Prefix + ":" + bar.Curs.TokenObj.theToken.Substring(1); // prepend the Localising prefix

                parseToValueFor(bar, "\"abstract\"");
                bar.Curs.TokenObj.theToken = "\"" + Prefix + ":" + bar.Curs.TokenObj.theToken.Substring(1); // prepend the Localising prefix

                // Now Replace download
                parseToValueFor(bar, "\"download\"");
                System.Uri DldURI = new System.Uri(DownloadPath);              // Convert it to a URI
                bar.Curs.TokenObj.theToken = "\"" + DldURI.AbsoluteUri + "\""; // replace the URI

                // Now Replace download_Size
                parseToValueFor(bar, "\"download_size\"");
                bar.Curs.TokenObj.theToken = DownloadSize.ToString();

                // Now Replace download_Size
                if (bar.hasANameField("\"x_generated_by\"") == true)
                {
                    parseToValueFor(bar, "\"x_generated_by\"");
                    bar.Curs.TokenObj.theToken = "\"CkanLocaliser\"";
                }
                // Now for the not so easy bits  Values in CompoundTypes.
                CkanLocaliserClass.DoingWhat.Pop();
                CkanLocaliserClass.DoingWhat.Push("Wiping_Resoruces_Links");
                killResourcesLinks(bar);

                CkanLocaliserClass.DoingWhat.Pop();
                CkanLocaliserClass.DoingWhat.Push("Fixing_Hashes");
                fixHashValues(bar);
                // Now for the hard bits
                // done backwards through the file.. just to feel safer
                CkanLocaliserClass.DoingWhat.Pop();

                // Insert "Conflicts" preceded by "Provides" just after "license"
                InsertProvidesConflicts(bar);
                // Insert an author either makign the string into an array and/or prepending our Author string

                CkanLocaliserClass.DoingWhat.Push("Adding_Author");

                PieceOfPaper p = new PieceOfPaper();
                p.valueToAdd        = Stringify(Author);
                p.listToAddItTo     = "\"author\"";
                p.thingToAdditAfter = new List <string>();
                p.thingToAdditAfter.Add("\"abstract\"");
                p.EndProvidesLineNo  = 0;
                p.EndProvidesTokenNo = 0;
                fixValueInList(bar, ref p);

                CkanLocaliserClass.DoingWhat.Pop();
            }
            catch (FormatException fe)
            {
                if (fe.KnownReal == false)
                {   // EG: we know it is real if we try to localise ckan file and it has no "identifier" Name:Value pair   etc.
                    Console.WriteLine("CAVEAT: There is reasonably good chance th following is a code error.");
                    Console.WriteLine("CAVEAT: The code has been mainly/entirely checking things we thoguht we just checked already.");
                    Console.WriteLine("CAVEAT: ALL examples of ckan files (schema valid or not) that do this when localised gratefully appreciated.");
                }
                Console.WriteLine($"While Doing : {CkanLocaliserClass.WhatString()} ");
                Console.WriteLine(fe.Cause);

                return(-1);
            }
            if (true)
            {
                using (StreamWriter sw = File.CreateText(args[3]))
                {
                    Foo.writeTo(sw);
                    sw.Close();
                }
            }
            CkanLocaliserClass.DoingWhat.Pop();

            return(0);
        }