Skip to content

Instantly share code, notes, and snippets.

@Amitkapadi
Forked from giacomelli/GistImporter.cs
Created June 28, 2023 07:17
Show Gist options
  • Select an option

  • Save Amitkapadi/53a3282ba05de36e6d6f12b99238b448 to your computer and use it in GitHub Desktop.

Select an option

Save Amitkapadi/53a3282ba05de36e6d6f12b99238b448 to your computer and use it in GitHub Desktop.

Revisions

  1. @giacomelli giacomelli revised this gist Aug 11, 2020. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions GistImporter.cs
    Original file line number Diff line number Diff line change
    @@ -11,12 +11,12 @@
    /// http://diegogiacomelli.com.br/unitytips-gist-importer
    /// </summary>
    [InitializeOnLoad]
    public static class GistImporter
    public static class GistImporter
    {
    const string GistsFolder = "Gists";
    static readonly Regex _getGistUrlInfoRegex = new Regex("https://gist.github.com/(?<owner>.+)/(?<gistId>[a-z0-9]+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    static readonly Regex _getDescriptionRegex = new Regex(@"\<title\>(?<description>.+)\</title\>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    static readonly Regex _getFileUrlRegex = new Regex("href=\"(?<url>.+/raw/.+)\"", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    static readonly Regex _getFileUrlRegex = new Regex("href=\"(?<url>.+/raw/[a-z0-9\\./\\-]+)\"", RegexOptions.IgnoreCase | RegexOptions.Compiled);

    [MenuItem("Tools/Import Gist")]
    static void ImportGist()
    @@ -77,8 +77,8 @@ static bool ValidateImportGist()
    static void DownloadFile(WebClient client, string url, string destinationFolder, string filename)
    {
    var fileContent = client.DownloadString(url);
    // Is an editor script?

    // Is an editor script?
    if (fileContent.IndexOf("using UnityEditor;", StringComparison.OrdinalIgnoreCase) > -1)
    destinationFolder = Path.Combine(destinationFolder, "Editor");

  2. @giacomelli giacomelli revised this gist Jun 24, 2019. 1 changed file with 13 additions and 9 deletions.
    22 changes: 13 additions & 9 deletions GistImporter.cs
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    using System;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text.RegularExpressions;
    using UnityEditor;
    @@ -36,19 +37,22 @@ static void ImportGist()
    var infoMatch = _getGistUrlInfoRegex.Match(gistUrl).Groups;
    var gistOwner = infoMatch["owner"].Value;
    var gistId = infoMatch["gistId"].Value;
    var destinationFolder = Path.Combine(Application.dataPath, GistsFolder, gistOwner);
    var rawUrls = filesMatches
    .OfType<Match>()
    .Select(m => $"https://gist.github.com{m.Groups["url"].Value}")
    .OrderByDescending(u => u.EndsWith(".cs", StringComparison.OrdinalIgnoreCase))
    .ToArray();

    var destinationFolder = Path.Combine(Application.dataPath, GistsFolder, gistOwner, $"{Path.GetFileNameWithoutExtension(rawUrls.First())} ({gistId})");

    // Downloads and write the Gist files.
    for (var i = 0; i < filesMatches.Count; i++)
    for (var i = 0; i < rawUrls.Length; i++)
    {
    var url = $"https://gist.github.com{filesMatches[i].Groups["url"].Value}";
    var filename = Path.GetFileName(url);

    if (i == 0)
    destinationFolder = Path.Combine(destinationFolder, $"{Path.GetFileNameWithoutExtension(filename)} ({gistId})");
    var rawUrl = rawUrls[i];
    var filename = Path.GetFileName(rawUrl);

    EditorUtility.DisplayProgressBar("Importing Gist...", filename, i / (float)filesMatches.Count);
    DownloadFile(client, url, destinationFolder, filename);
    DownloadFile(client, rawUrl, destinationFolder, filename);
    }

    EditorUtility.ClearProgressBar();
    @@ -86,7 +90,7 @@ static void WriteReadme(string gistUrl, string gistPageContent, string folder)
    {
    var description = _getDescriptionRegex.Match(gistPageContent).Groups["description"].Value;
    var readmePath = Path.Combine(folder, "readme.txt");
    File.WriteAllText(readmePath, $"{description}\n\nUrl: {gistUrl}\nDate: {DateTime.Now:dd/MM/yyyy HH:mm}\n\nImported using {nameof(GistImporter)}(http://diegogiacomelli.com.br/unitytips-gist-importer).");
    File.WriteAllText(readmePath, $"{description}\n\nUrl: {gistUrl}\nDate: {DateTime.Now:dd/MM/yyyy HH:mm}\n\nImported using Gist Importer (http://diegogiacomelli.com.br/unitytips-gist-importer).");

    // Selects the readme file on project window.
    AssetDatabase.Refresh();
  3. @giacomelli giacomelli revised this gist Jun 24, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion GistImporter.cs
    Original file line number Diff line number Diff line change
    @@ -86,7 +86,7 @@ static void WriteReadme(string gistUrl, string gistPageContent, string folder)
    {
    var description = _getDescriptionRegex.Match(gistPageContent).Groups["description"].Value;
    var readmePath = Path.Combine(folder, "readme.txt");
    File.WriteAllText(readmePath, $"Gist imported using {nameof(GistImporter)}:\n\n{description}\n\nUrl: {gistUrl}\nDate: {DateTime.Now:dd/MM/yyyy HH:mm}");
    File.WriteAllText(readmePath, $"{description}\n\nUrl: {gistUrl}\nDate: {DateTime.Now:dd/MM/yyyy HH:mm}\n\nImported using {nameof(GistImporter)}(http://diegogiacomelli.com.br/unitytips-gist-importer).");

    // Selects the readme file on project window.
    AssetDatabase.Refresh();
  4. @giacomelli giacomelli created this gist Jun 24, 2019.
    97 changes: 97 additions & 0 deletions GistImporter.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,97 @@
    using System;
    using System.IO;
    using System.Net;
    using System.Text.RegularExpressions;
    using UnityEditor;
    using UnityEngine;

    /// <summary>
    /// Gist importer.
    /// http://diegogiacomelli.com.br/unitytips-gist-importer
    /// </summary>
    [InitializeOnLoad]
    public static class GistImporter
    {
    const string GistsFolder = "Gists";
    static readonly Regex _getGistUrlInfoRegex = new Regex("https://gist.github.com/(?<owner>.+)/(?<gistId>[a-z0-9]+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    static readonly Regex _getDescriptionRegex = new Regex(@"\<title\>(?<description>.+)\</title\>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    static readonly Regex _getFileUrlRegex = new Regex("href=\"(?<url>.+/raw/.+)\"", RegexOptions.IgnoreCase | RegexOptions.Compiled);

    [MenuItem("Tools/Import Gist")]
    static void ImportGist()
    {
    var gistUrl = EditorGUIUtility.systemCopyBuffer;

    try
    {
    using (var client = new WebClient())
    {
    var gistPageContent = client.DownloadString(gistUrl);

    // Extract the files raw urls.
    var filesMatches = _getFileUrlRegex.Matches(gistPageContent);

    if (filesMatches.Count > 0)
    {
    var infoMatch = _getGistUrlInfoRegex.Match(gistUrl).Groups;
    var gistOwner = infoMatch["owner"].Value;
    var gistId = infoMatch["gistId"].Value;
    var destinationFolder = Path.Combine(Application.dataPath, GistsFolder, gistOwner);

    // Downloads and write the Gist files.
    for (var i = 0; i < filesMatches.Count; i++)
    {
    var url = $"https://gist.github.com{filesMatches[i].Groups["url"].Value}";
    var filename = Path.GetFileName(url);

    if (i == 0)
    destinationFolder = Path.Combine(destinationFolder, $"{Path.GetFileNameWithoutExtension(filename)} ({gistId})");

    EditorUtility.DisplayProgressBar("Importing Gist...", filename, i / (float)filesMatches.Count);
    DownloadFile(client, url, destinationFolder, filename);
    }

    EditorUtility.ClearProgressBar();
    WriteReadme(gistUrl, gistPageContent, destinationFolder);
    }
    else
    Debug.LogWarning($"No files found for Gist '{gistUrl}'.");
    }
    }
    finally
    {
    EditorUtility.ClearProgressBar();
    }
    }

    [MenuItem("Tools/Import Gist", true)]
    static bool ValidateImportGist()
    {
    return _getGistUrlInfoRegex.IsMatch(EditorGUIUtility.systemCopyBuffer);
    }

    static void DownloadFile(WebClient client, string url, string destinationFolder, string filename)
    {
    var fileContent = client.DownloadString(url);

    // Is an editor script?
    if (fileContent.IndexOf("using UnityEditor;", StringComparison.OrdinalIgnoreCase) > -1)
    destinationFolder = Path.Combine(destinationFolder, "Editor");

    Directory.CreateDirectory(destinationFolder);
    File.WriteAllText(Path.Combine(destinationFolder, filename), fileContent);
    }

    static void WriteReadme(string gistUrl, string gistPageContent, string folder)
    {
    var description = _getDescriptionRegex.Match(gistPageContent).Groups["description"].Value;
    var readmePath = Path.Combine(folder, "readme.txt");
    File.WriteAllText(readmePath, $"Gist imported using {nameof(GistImporter)}:\n\n{description}\n\nUrl: {gistUrl}\nDate: {DateTime.Now:dd/MM/yyyy HH:mm}");

    // Selects the readme file on project window.
    AssetDatabase.Refresh();
    var readmeAsset = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>($"Assets{readmePath.Replace(Application.dataPath, string.Empty)}");
    Selection.activeObject = readmeAsset;
    EditorGUIUtility.PingObject(readmeAsset);
    }
    }