EloiStree / CodeAndQuestsEveryDay

Regular research on the Quest for developers.
http://codeandquests.page.link/discord
32 stars 3 forks source link

Daily Log: Day 38, 2019_06_30 #306

Open EloiStree opened 5 years ago

EloiStree commented 5 years ago

Objective

Done

EloiStree commented 5 years ago

Version 1 of a tool to create your pacakage manager:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using UnityEngine;

public class CreatePackageStructure : MonoBehaviour
{
    public string m_path;
    public bool m_generateFiles;
    public string m_commitDescription;
    public bool m_addCommitPush;

    [Header("Package Info")]
    public string m_packageName = "Package name";
    public string m_packageNameId = "packagename";
    public string m_nameId = "be.eloiexperiments.packagename";
    public string m_version = "0.0.1";
    public string m_unityVersion = "2018.1";
    [TextArea(1,5)]
    public string m_description = "";
    public string [] m_keywords = new string[] { "Tool", "Script","" ,"" };
    public string m_category = "Script";
    public PackageDepencie[] m_dependencies = new PackageDepencie[] {
        new PackageDepencie("be.eloiexepriments.randomtool","https://gitlab.com/eloistree/2019_01_15_randomtool.git")
    };

    [TextArea(1, 20)]
    public string m_packageFile;
    [TextArea(1, 20)]
    public string m_runtimeFile;
    [TextArea(1, 20)]
    public string m_editorFile;

    [System.Serializable]
    public class PackageDepencie {
        public string m_namespace="be.eloiexepriments.packagename";
        public string m_gitRepoURL = "https://gitlab.com/eloistree/___.git";
        public PackageDepencie(string name, string gitUrl)
        {
            m_namespace = name;
            m_gitRepoURL = gitUrl;
        }

    }

    public void Generate() {

        CreatePackageDescription();

#if UNITY_EDITOR

        UnityEditor.AssetDatabase.Refresh();

#endif

    }
    public void OnValidate() {

        if (m_commitDescription == "")
            m_commitDescription = "None";
        RefreshInfo_PackageDescription();
        RefreshInfo_PackageRuntime();
        RefreshInfo_PackageEditor();

        if (m_generateFiles)
        {
            m_generateFiles = false;
            Generate();

        }
        if (m_addCommitPush)
        {
            m_addCommitPush = false;
            AddCommitPush();

        }

    }
    //https://stackoverflow.com/a/54016996s
    static void RunCommands(List<string> cmds, string workingDirectory = "")
    {
        var process = new Process();
        var psi = new ProcessStartInfo();
        psi.FileName = "cmd.exe";
        psi.RedirectStandardInput = true;
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardError = true;
        psi.UseShellExecute = false;
        psi.WorkingDirectory = workingDirectory;
        process.StartInfo = psi;
        process.Start();
        process.OutputDataReceived += (sender, e) => { Console.WriteLine(e.Data); };
        process.ErrorDataReceived += (sender, e) => { Console.WriteLine(e.Data); };
        process.BeginOutputReadLine();
        process.BeginErrorReadLine();
        using (StreamWriter sw = process.StandardInput)
        {
            foreach (var cmd in cmds)
            {
                sw.WriteLine(cmd);
            }
        }
        process.WaitForExit();
    }
    private void AddCommitPush()
    {

        //https://stackoverflow.com/questions/26167387/run-git-commands-from-a-c-sharp-function
        string[] cmds = new string[] {
            "git add -A",
            "git commit -m \""+m_commitDescription+"\"",
            "git push"
        };

        RunCommands(cmds.ToList() , m_path);

    }

    private void RefreshInfo_PackageRuntime()
    {
        string dependencies = "";
        if (m_dependencies.Length > 0)
        {
            for (int i = 0; i < m_dependencies.Length; i++)
            {
                dependencies += "\""+m_dependencies[i].m_namespace+"\"" +  (i == m_dependencies.Length - 1 ? "" : ",") + "\n";
            }
        }
        string runtime = ""
        + "{\n"
        + "    \"name\": \"" + m_nameId + "\",\n"
        + "    \"references\": [\n"
        + dependencies
        + "],"
        + "    \"optionalUnityReferences\": [],\n"
        + "    \"includePlatforms\": [],\n"
        + "    \"excludePlatforms\": [],\n"
        + "    \"allowUnsafeCode\": false,\n"
        + "    \"overrideReferences\": false,\n"
        + "    \"precompiledReferences\": [],\n"
        + "    \"autoReferenced\": true,\n"
        + "    \"defineConstraints\": []\n"
        + "}\n";
        m_runtimeFile = runtime;
    }
    private void RefreshInfo_PackageEditor( )
    {
        string editor = ""
        + " {\n"
        + " \"name\": \"" + m_nameId + "editor\",\n"
        + " \"references\": [\n"
        + "     \"" + m_nameId + "\"\n"
        + " ],\n"
        + " \"optionalUnityReferences\": [],\n"
        + " \"includePlatforms\": [\n"
        + "     \"Editor\"\n"
        + " ],\n"
        + " \"excludePlatforms\": [],\n"
        + " \"allowUnsafeCode\": false,\n"
        + " \"overrideReferences\": false,\n"
        + " \"precompiledReferences\": [],\n"
        + " \"autoReferenced\": true,\n"
        + " \"defineConstraints\": []\n"
        + " }\n";
        m_editorFile = editor;
    }
    private void RefreshInfo_PackageDescription()
    {

        string keywords = "";
        if (m_keywords.Length > 0)
        {
            keywords = "\"" + string.Join("\",\n\"", m_keywords) + "\"\n";
        }

        string dependencies = "";
        if (m_dependencies.Length > 0)
        {
            for (int i = 0; i < m_dependencies.Length; i++)
            {
                dependencies +=string.Format("\"{0}\":\"{1}\"", m_dependencies[i].m_namespace, m_dependencies[i].m_gitRepoURL) + (i== m_dependencies.Length-1 ?"":",")+"\n" ;
            }
        }

        string formatFile = ""

       + "{\n"
       + "            \"name\": \""+ m_nameId + "\",\n"
       + "  \"displayName\": \"" + m_packageName + "\",\n"
       + "  \"version\": \"" + m_version  + "\",\n"
       + "  \"unity\": \"" + m_unityVersion + "\",\n"
       + "  \"description\": \"" + m_description + "\",\n"
       + "  \"keywords\": [\n"
       + keywords
       //+ "    \"Random\","
       //+ "    \"Tool\","
       //+ "    \"Unity\""
       + "  ],\n"
       + "  \"category\": \"Script\",\n"
       + "  \"dependencies\": {\n"
       + dependencies
       //+ "     \"be.eloiexperiments.randomtool\": \"https://gitlab.com/eloistree/2019_01_15_randomtool.git\""
       + "        }\n"
       + "}\n"
       + "";

        m_packageFile = formatFile;

    }

    public void Reset()
    {
        m_path = Application.dataPath+"/packagename";
    }

    private void CreatePackageDescription()
    {
        File.WriteAllText(m_path + "/package.json", m_packageFile);

        Directory.CreateDirectory(m_path + "/Editor/");
        Directory.CreateDirectory(m_path + "/Editor/Script");
        File.WriteAllText(m_path + "/Editor/" + "com.unity." + m_packageNameId + ".Editor.asmdef", m_editorFile );

        Directory.CreateDirectory(m_path + "/Runtime/");
        Directory.CreateDirectory(m_path + "/Runtime/Script");
        File.WriteAllText(m_path + "/Runtime/" + "com.unity." + m_packageNameId + ".Runtime.asmdef", m_runtimeFile);
    }
}