Example #1
0
        public override Task <Dictionary <string, object> > HandleRequest(Dictionary <string, object> request)
        {
            switch ((string)request["command"])
            {
            case "GetAssemblyMetadataFromAnyPossibleKey":
                string                      locale        = request.ContainsKey("locale") ? (string)request["locale"] : null;
                string                      name          = (string)request["name"];
                string[]                    localDeps     = (string[])request["localDeps"];
                string                      projectDir    = (string)request["projectDir"];
                bool                        includeSource = (bool)request["includeSource"];
                AssemblyFinder              af            = this.GetAssemblyFinder(localDeps, projectDir, this.Hub.SourceRoot);
                InternalAssemblyMetadata    md            = af.GetAssemblyMetadataFromAnyPossibleKey(locale == null ? name : (locale + ":" + name));
                Dictionary <string, object> output        = new Dictionary <string, object>();
                if (md == null)
                {
                    output["found"] = false;
                }
                else
                {
                    output["found"]            = true;
                    output["id"]               = md.ID;
                    output["internalLocale"]   = md.InternalLocale.ID;
                    output["supportedLocales"] = md.SupportedLocales.Select(loc => loc.ID).OrderBy(k => k).ToArray();
                    List <string> nameByLocale = new List <string>();
                    foreach (string loc in md.NameByLocale.Keys)
                    {
                        nameByLocale.Add(loc);
                        nameByLocale.Add(md.NameByLocale[loc]);
                    }
                    output["nameByLocale"] = nameByLocale.ToArray();
                    List <string> onlyImportableFrom = new List <string>();
                    output["onlyImportableFrom"] = md.OnlyImportableFrom;

                    if (includeSource)
                    {
                        Dictionary <string, string> code = md.GetSourceCode();
                        List <string> codeOut            = new List <string>();
                        foreach (string file in code.Keys.OrderBy(k => k))
                        {
                            codeOut.Add(file);
                            codeOut.Add(code[file]);
                        }
                        output["sourceCode"] = codeOut.ToArray();
                    }
                }

                return(Task.FromResult(output));

            default:
                throw new NotImplementedException();
            }
        }
Example #2
0
        private static InternalAssemblyMetadata[] GetAvailableLibraryPathsByLibraryName(
            string[] nullableBuildFileLocalDepsList,
            string[] libraryDirectories,
            string nullableProjectDirectory)
        {
            List <string> unverifiedLibraryDirectories = new List <string>();

            if (nullableBuildFileLocalDepsList != null)
            {
                foreach (string localDep in nullableBuildFileLocalDepsList)
                {
                    string localDepAbsolute = FileUtil.GetAbsolutePathFromRelativeOrAbsolutePath(nullableProjectDirectory, localDep);
                    unverifiedLibraryDirectories.Add(localDepAbsolute);
                }
            }

            foreach (string registeredLibraryPath in libraryDirectories)
            {
                unverifiedLibraryDirectories.AddRange(FileUtil.DirectoryListDirectoryPaths(registeredLibraryPath));
            }

            List <string> verifiedLibraryPaths = new List <string>();

            foreach (string dir in unverifiedLibraryDirectories)
            {
                string manifestPath = FileUtil.JoinPath(dir, "manifest.json");
                if (FileUtil.FileExists(manifestPath))
                {
                    verifiedLibraryPaths.Add(dir);
                }
            }

            // Library name collisions will override any previous definition.
            // For example, a custom library referenced by a build file will override a built-in library.
            // An example use case of this would be to define a custom library called "Gamepad" for mobile that puts
            // buttons in the corners of the screen, but without having to change any code to be platform-aware.
            Dictionary <string, InternalAssemblyMetadata> uniqueAssemblies = new Dictionary <string, InternalAssemblyMetadata>();

            foreach (string path in verifiedLibraryPaths.Reverse <string>())
            {
                string defaultName = Path.GetFileName(path);
                InternalAssemblyMetadata metadata = AssemblyMetadataFactory.CreateLibrary(path, defaultName);

                // TODO: don't hardcode EN
                string uniqueKey = "en:" + metadata.ID;
                uniqueAssemblies[uniqueKey] = metadata;
            }

            return(uniqueAssemblies.Values
                   .OrderBy(metadata => metadata.ID.ToLowerInvariant())
                   .ToArray());
        }
Example #3
0
        public static InternalAssemblyMetadata CreateLibrary(string directory, string id)
        {
            Wax.Util.JsonLookup manifest;
            string manifestText = FileUtil.ReadFileText(FileUtil.JoinPath(directory, "manifest.json"));

            try
            {
                manifest = new Wax.Util.JsonLookup(new Wax.Util.JsonParser(manifestText)
                                                   .AddOption(Wax.Util.JsonOption.ALLOW_TRAILING_COMMA)
                                                   .AddOption(Wax.Util.JsonOption.ALLOW_COMMENTS)
                                                   .ParseAsDictionary());
            }
            catch (Wax.Util.JsonParser.JsonParserException jpe)
            {
                throw new System.InvalidOperationException("Syntax error while parsing the library manifest for '" + id + "'.", jpe);
            }

            Locale internalLocale      = Locale.Get(manifest.GetAsString("localization.default", "en"));
            InternalAssemblyMetadata m = new InternalAssemblyMetadata(id, internalLocale, directory);

            IDictionary <string, object> namesByLocale = manifest.GetAsDictionary("localization.names");

            foreach (string localeId in namesByLocale.Keys)
            {
                string name = namesByLocale[localeId] as string;
                m.NameByLocale[localeId] = name ?? m.ID;
            }

            HashSet <Locale> supportedLocales = new HashSet <Locale>(manifest.GetAsDictionary("localization.names").Keys.Select(localeName => Locale.Get(localeName)));

            supportedLocales.Add(m.InternalLocale);
            m.SupportedLocales = supportedLocales.OrderBy(loc => loc.ID).ToArray();

            m.OnlyImportableFrom = new HashSet <string>(manifest.GetAsList("onlyAllowImportFrom").Cast <string>()).ToArray();

            return(m);
        }