示例#1
0
        private async void CreateAt(string path)
        {
            var myPkgInfo = await GetMyPackageInfoAsync();

            var readmeTemplatePath  = $"{myPkgInfo.assetPath}/Template/README_TEMPLATE.md";
            var licenseTemplatePath = $"{myPkgInfo.assetPath}/Template/Licenses/MIT LICENSE";

            if (!Directory.Exists(path))
            {
                EditorUtility.DisplayDialog("Error", "Unable to locate packages directory.", "OK");
                return;
            }

            var packagePath = $"{path}/{packageManifest.repositoryName}";

            if (Directory.Exists(packagePath))
            {
                EditorUtility.DisplayDialog("Error", "A Package with that name already exists.", "OK");
                return;
            }

            var json = EditorJsonUtility.ToJson(packageManifest, true);

            Directory.CreateDirectory(packagePath);
            Directory.CreateDirectory($"{packagePath}/Editor");
            Directory.CreateDirectory($"{packagePath}/Runtime");
            Directory.CreateDirectory($"{packagePath}/Tests/Editor");
            Directory.CreateDirectory($"{packagePath}/Tests/Runtime");

            var manifestPath = $"{packagePath}/package.json";
            var readmePath   = $"{packagePath}/README.md";
            var licensePath  = $"{packagePath}/LICENSE.md";

            var readmeText  = PackageUtility.CreateReadmeText(readmeTemplatePath, packageManifest);
            var licenseText = PackageUtility.CreateLicenseText(File.ReadAllText(licenseTemplatePath), packageManifest);

            File.WriteAllText(manifestPath, json);
            File.WriteAllText(readmePath, readmeText);
            File.WriteAllText(licensePath, licenseText);

            var assemblyName           = packageManifest.name;
            var editorAssemblyName     = $"{packageManifest.name}.Editor";
            var testAssemblyName       = $"{assemblyName}.Tests";
            var testEditorAssemblyName = $"{editorAssemblyName}.Tests";

            var assemblyDefPath           = $"{packagePath}/Editor/{editorAssemblyName}.asmdef";
            var editorAssemblyDefPath     = $"{packagePath}/Runtime/{assemblyName}.asmdef";
            var testEditorAssemblyDefPath = $"{packagePath}/Tests/Editor/{testEditorAssemblyName}.asmdef";
            var testAssemblyDefPath       = $"{packagePath}/Tests/Runtime/{testAssemblyName}.asmdef";

            File.WriteAllText(assemblyDefPath,
                              $"{{ \"name\": \"{editorAssemblyName}\", \"references\": [ \"{assemblyName}\" ], \"optionalUnityReferences\": [], \"includePlatforms\": [ \"Editor\" ], \"excludePlatforms\": [] }}");
            File.WriteAllText(editorAssemblyDefPath, $"{{ \"name\": \"{assemblyName}\" }}");

            File.WriteAllText(testEditorAssemblyDefPath,
                              $"{{ \"name\": \"{testEditorAssemblyName}\", \"references\": [ \"{assemblyName}\" ], \"optionalUnityReferences\": [\"TestAssemblies\"], \"includePlatforms\": [ \"Editor\" ], \"excludePlatforms\": [] }}");
            File.WriteAllText(testAssemblyDefPath,
                              $"{{ \"name\": \"{testAssemblyName}\", \"references\": [ \"{assemblyName}\" ], \"optionalUnityReferences\": [\"TestAssemblies\"], \"includePlatforms\": [], \"excludePlatforms\": [] }}");

            AssetDatabase.Refresh();
            EditorUtility.DisplayDialog("Package Created", "Done!", "Ok");
            Refresh();
        }
示例#2
0
        private async void UpdateReadme(PackageInfo packageInfo)
        {
            var myPkg = await GetMyPackageInfoAsync();

            var readmeTemplatePath = $"{myPkg.assetPath}/Template/README_TEMPLATE.md";

            var readmePath = $"{packageInfo.assetPath}/README";

            if (!File.Exists(readmePath))
            {
                readmePath = $"{packageInfo.assetPath}/README.md";
                if (!File.Exists(readmePath))
                {
                    Debug.LogError("Unable to find README or README.md at package asset path");
                    return;
                }
            }

            var manifestPath = $"{packageInfo.assetPath}/package.json";

            if (!File.Exists(manifestPath))
            {
                Debug.LogError($"Unable to find package.json at {packageInfo.assetPath}");
                return;
            }

            PackageManifest packageManifest = null;

            try
            {
                packageManifest = JsonUtility.FromJson <PackageManifest>(File.ReadAllText(manifestPath));
                if (packageManifest == null)
                {
                    Debug.LogError("Failed to read package manifest. FromJson returned null on file text.");
                    return;
                }
            }
            catch (Exception e)
            {
                Debug.LogError("Failed to read package manifest format.");
                Debug.LogException(e);
                return;
            }

            if (!ValidatePackageManifest(packageManifest))
            {
                Debug.LogError("Update package manifest with required values before updating the readme");
                return;
            }

            var oldText      = File.ReadAllText(readmePath);
            var templateText = File.ReadAllText(readmeTemplatePath);

            templateText = PackageUtility.PatchReadmeText(oldText, templateText);

            var readmeText = PackageUtility.CreateReadmeText(templateText, packageManifest);

            File.WriteAllText(readmePath, readmeText);

            EditorUtility.DisplayDialog("Update Readme", "Done", "OK");
        }