Skip to content

Commit

Permalink
cleanup implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JoC0de committed Dec 30, 2022
1 parent 9d60da5 commit 09d4ee0
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions Assets/NuGet/Editor/NugetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,13 @@ private static void Clean(NugetPackageIdentifier package)
DeleteDirectory(packageInstallDirectory + "/StreamingAssets");
DeleteFile(packageInstallDirectory + "/StreamingAssets.meta");
}

// if there are roslyn analyzers we need to modify its import settings
var analyzersDirectory = Path.Combine(packageInstallDirectory, "analyzers");
if (Directory.Exists(analyzersDirectory))
{
ModifyImportSettingsOfRoslynAnalyzers(analyzersDirectory);
}
}

private static bool IsAlreadyImportedInEngine(NugetPackageIdentifier package)
Expand Down Expand Up @@ -1448,46 +1455,55 @@ public static bool Install(NugetPackage package, bool refreshAssets = true)
{
EditorUtility.DisplayProgressBar(string.Format("Installing {0} {1}", package.Id, package.Version), "Importing Package", 0.95f);
AssetDatabase.Refresh();
ModifyImportSettingsIfRoslynAnalyzer(package.Id, package.Version);
EditorUtility.ClearProgressBar();
}
}
return installSuccess;
}

private static void ModifyImportSettingsIfRoslynAnalyzer(string id, string version)
private static void ModifyImportSettingsOfRoslynAnalyzers(string analyzersDirectory)
{
var dir = Path.Combine(NugetConfigFile.RepositoryPath, $"{id}.{version}");
if (!Directory.Exists(dir))
foreach (var analyzer in Directory.GetFiles(analyzersDirectory, "*.dll", SearchOption.AllDirectories))
{
Debug.LogError($"Directory {dir} not found.");
return;
}

foreach (var analyzer in Directory.GetFiles(dir, "*.dll", SearchOption.AllDirectories)
.Where(x => x.Contains("/analyzers/")))
{
// Trigger Unity to create the meta file, the path for ImportAsset MUST be relative to the project path
var projectRelativePath = analyzer.Substring(new DirectoryInfo(Application.dataPath).Parent.FullName.Length + 1);
// Trigger Unity to create the .meta file, the path for ImportAsset MUST be relative to the project path
var projectRelativePath = GetProjectRelativePath(analyzer);
AssetDatabase.ImportAsset(projectRelativePath, ImportAssetOptions.ForceSynchronousImport);
var meta = AssetImporter.GetAtPath(projectRelativePath) as PluginImporter;
if (meta == null)
{
Debug.LogWarning($".meta file not found: {analyzer}");
Debug.LogWarning($".meta file not found for: '{analyzer}'");
continue;
}

meta.SetCompatibleWithAnyPlatform(false);
meta.SetCompatibleWithEditor(false);
foreach (var platform in Enum.GetValues(typeof(BuildTarget)))
{
meta.SetExcludeFromAnyPlatform((BuildTarget) platform, false);
meta.SetExcludeFromAnyPlatform((BuildTarget)platform, false);
}

AssetDatabase.SetLabels(meta, new[] {"RoslynAnalyzer"});
AssetDatabase.SetLabels(meta, new[] { "RoslynAnalyzer" });
}
}

private static string GetProjectRelativePath(string path)
{
if (path == null)
{
return null;
}

// Path.GetRelativePath is only available in newer .net versions so we need to implement it our self
path = Path.GetFullPath(path);
var projectPath = Path.GetFullPath(Path.GetDirectoryName(Application.dataPath)).TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
if (!path.StartsWith(projectPath, StringComparison.OrdinalIgnoreCase))
{
return path;
}

return path.Substring(projectPath.Length);
}

private static void WarnIfDotNetAuthenticationIssue(Exception e)
{
#if !NET_4_6
Expand Down

0 comments on commit 09d4ee0

Please sign in to comment.