Skip to content

Use productName as Solution if available + Merge patch and generate logic for settings.json #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Packages/com.unity.ide.visualstudio.tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "com.unity.ide.visualstudio.tests",
"displayName": "Visual Studio Editor",
"description": "Code editor integration for supporting Visual Studio as code editor for unity. Adds support for generating csproj files for intellisense purposes, auto discovery of installations, etc.",
"version": "2.0.21",
"version": "2.0.22",
"unity": "2019.4",
"unityRelease": "25f1",
"dependencies": {
Expand Down
15 changes: 15 additions & 0 deletions Packages/com.unity.ide.visualstudio/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Code Editor Package for Visual Studio

## [2.0.22] - 2023-10-03

Integration:

- Add support for `XDG_DATA_DIRS` and `.desktop` files on Linux for `VS Code` discovery.
- Use compile-time platform-specifics instead of using runtime conditions.

Project generation:

- Suppress `USG0001` warnings.
- Mark referenced assemblies as private (to not copy extra files to output directory when building).
- Add Unity capability to SDK-Style projects.
- Prevent circular dependency errors with SDK-Style projects.


## [2.0.21] - 2023-09-05

Integration:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,31 +151,39 @@ public bool IsInternalizedPackagePath(string path)
{
return false;
}

var packageInfo = FindForAssetPath(path);
if (packageInfo == null)
{
return false;
}
var packageSource = packageInfo.source;

var flag = ProjectGenerationFlagFromPackageSource(packageInfo.source);

return flag != ProjectGenerationFlag.None && !ProjectGenerationFlag.HasFlag(flag);
}

private static ProjectGenerationFlag ProjectGenerationFlagFromPackageSource(PackageSource packageSource)
{
switch (packageSource)
{
case PackageSource.Embedded:
return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Embedded);
return ProjectGenerationFlag.Embedded;
case PackageSource.Registry:
return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Registry);
return ProjectGenerationFlag.Registry;
case PackageSource.BuiltIn:
return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.BuiltIn);
return ProjectGenerationFlag.BuiltIn;
case PackageSource.Unknown:
return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Unknown);
return ProjectGenerationFlag.Unknown;
case PackageSource.Local:
return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Local);
return ProjectGenerationFlag.Local;
case PackageSource.Git:
return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Git);
return ProjectGenerationFlag.Git;
case PackageSource.LocalTarball:
return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.LocalTarBall);
return ProjectGenerationFlag.LocalTarBall;
default:
return ProjectGenerationFlag.None;
}

return false;
}

public ResponseFileData ParseResponseFile(string responseFilePath, string projectDirectory, string[] systemReferenceDirectories)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ProjectGeneration() : this(Directory.GetParent(Application.dataPath).Full
public ProjectGeneration(string tempDirectory, IAssemblyNameProvider assemblyNameProvider, IFileIO fileIoProvider, IGUIDGenerator guidGenerator)
{
ProjectDirectory = FileUtility.NormalizeWindowsToUnix(tempDirectory);
m_ProjectName = Path.GetFileName(ProjectDirectory);
m_ProjectName = string.IsNullOrEmpty(PlayerSettings.productName) ? Path.GetFileName(ProjectDirectory) : PlayerSettings.productName;
m_AssemblyNameProvider = assemblyNameProvider;
m_FileIOProvider = fileIoProvider;
m_GUIDGenerator = guidGenerator;
Expand Down Expand Up @@ -640,7 +640,7 @@ private static IEnumerable<string> GetOtherArguments(ResponseFileData[] response
var key = argument
.Substring(1, index - 1)
.Trim();

if (!names.Contains(key))
continue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,7 @@ public override string[] GetAnalyzers()

return GetAnalyzers(vstuPath); }

public override IGenerator ProjectGenerator
{
get
{
return _generator;
}
}
public override IGenerator ProjectGenerator => _generator;

private static bool IsCandidateForDiscovery(string path)
{
Expand Down Expand Up @@ -192,7 +186,7 @@ private static IEnumerable<string> GetXdgCandidates()
var desktopFile = IOPath.Combine(dir, "applications/code.desktop");
if (!File.Exists(desktopFile))
continue;

var content = File.ReadAllText(desktopFile);
match = DesktopFileExecEntry.Match(content);
}
Expand Down Expand Up @@ -243,7 +237,7 @@ public override void CreateExtraFiles(string projectDirectory)
}
catch (IOException)
{
}
}
}

private const string DefaultLaunchFileContent = @"{
Expand Down Expand Up @@ -302,99 +296,136 @@ private static void PatchLaunchFile(string launchFile)
}
}

static int Patch(JSONObject jobj,string key,bool value)
{
if(jobj.GetValueOrDefault(key,null) is JSONNode node && node.IsBoolean)
return 0;

jobj[key] = value;
return 1;
}

private void CreateSettingsFile(string vscodeDirectory, bool enablePatch)
{
var settingsFile = IOPath.Combine(vscodeDirectory, "settings.json");
if (File.Exists(settingsFile))
JSONObject settings = null;
try
{
if (enablePatch)
PatchSettingsFile(settingsFile);
if (File.Exists(settingsFile))
{
if (!enablePatch)
return;

return;
var content = File.ReadAllText(settingsFile);
settings = JSONNode.Parse(content) as JSONObject;
}
}
catch
{

const string excludes = @" ""files.exclude"": {
""**/.DS_Store"": true,
""**/.git"": true,
""**/.vs"": true,
""**/.gitmodules"": true,
""**/.vsconfig"": true,
""**/*.booproj"": true,
""**/*.pidb"": true,
""**/*.suo"": true,
""**/*.user"": true,
""**/*.userprefs"": true,
""**/*.unityproj"": true,
""**/*.dll"": true,
""**/*.exe"": true,
""**/*.pdf"": true,
""**/*.mid"": true,
""**/*.midi"": true,
""**/*.wav"": true,
""**/*.gif"": true,
""**/*.ico"": true,
""**/*.jpg"": true,
""**/*.jpeg"": true,
""**/*.png"": true,
""**/*.psd"": true,
""**/*.tga"": true,
""**/*.tif"": true,
""**/*.tiff"": true,
""**/*.3ds"": true,
""**/*.3DS"": true,
""**/*.fbx"": true,
""**/*.FBX"": true,
""**/*.lxo"": true,
""**/*.LXO"": true,
""**/*.ma"": true,
""**/*.MA"": true,
""**/*.obj"": true,
""**/*.OBJ"": true,
""**/*.asset"": true,
""**/*.cubemap"": true,
""**/*.flare"": true,
""**/*.mat"": true,
""**/*.meta"": true,
""**/*.prefab"": true,
""**/*.unity"": true,
""build/"": true,
""Build/"": true,
""Library/"": true,
""library/"": true,
""obj/"": true,
""Obj/"": true,
""Logs/"": true,
""logs/"": true,
""ProjectSettings/"": true,
""UserSettings/"": true,
""temp/"": true,
""Temp/"": true
}";

var content = @"{
" + excludes + @",
""dotnet.defaultSolution"": """ + IOPath.GetFileName(ProjectGenerator.SolutionFile()) + @"""
}";
}

File.WriteAllText(settingsFile, content);
}
string[] commonHiddenFolderExclude = new [] {
".DS_Store",
".git",
".vs",
".gitmodules",
".vsconfig",
};

string[] commonFileExtensionsExclude = new []{
"booproj",
"pidb",
"suo",
"user",
"userprefs",
"unityproj",
"dll",
"exe",
"pdf",
};

string[] binaryFileExtensionsExclude = new []{
"mid",
"midi",
"wav",
"gif",
"ico",
"jpg",
"jpeg",
"png",
"psd",
"tga",
"tif",
"tiff",
"3ds",
"3DS",
"fbx",
"FBX",
"lxo",
"LXO",
"ma",
"MA",
"obj",
"OBJ",
"cubemap",
"flare",
};

string[] unityFileExtensionsExclude = new []{
"asset",
"mat",
"meta",
"prefab",
"unity",
};

string[] unityFoldersExclude = new [] {
"Build",
"Library",
"Obj",
"Logs",
"ProjectSettings",
"UserSettings",
"Temp"
};

private void PatchSettingsFile(string settingsFile)
{
try
{
const string excludesKey = "files.exclude";
const string solutionKey = "dotnet.defaultSolution";

var content = File.ReadAllText(settingsFile);
var settings = JSONNode.Parse(content);
if(settings == null)
settings = new JSONObject();

var excludes = settings[excludesKey] as JSONObject;
if (excludes == null)
return;
settings[excludesKey] = excludes = new JSONObject();

int patched = 0;

foreach(string key in commonHiddenFolderExclude)
patched += Patch(excludes,"**/" + key,true);

foreach(string key in commonFileExtensionsExclude)
patched += Patch(excludes,"**/*." + key,true);

foreach(string key in binaryFileExtensionsExclude)
patched += Patch(excludes,"**/*." + key,true);

foreach(string key in unityFileExtensionsExclude)
patched += Patch(excludes,"**/*." + key,true);

foreach(string key in unityFoldersExclude)
{
string lower = key.ToLower();
if(key != lower)
patched += Patch(excludes,lower + "/",excludes.GetValueOrDefault(key + "/",true));

patched += Patch(excludes,key + "/",true);
}

var patchList = new List<string>();
var patched = false;

// Remove files.exclude for solution+project files in the project root
foreach (var exclude in excludes)
Expand All @@ -411,7 +442,7 @@ private void PatchSettingsFile(string settingsFile)
continue;

patchList.Add(key);
patched = true;
patched++;
}

// Check default solution
Expand All @@ -420,20 +451,23 @@ private void PatchSettingsFile(string settingsFile)
if (defaultSolution == null || defaultSolution.Value != solutionFile)
{
settings[solutionKey] = solutionFile;
patched = true;
patched++;
}

if (!patched)
if (patched < 1)
return;

foreach (var patch in patchList)
excludes.Remove(patch);

WriteAllTextFromJObject(settingsFile, settings);

UnityEngine.Debug.Log("patch counter : " + patched);
}
catch (Exception)
catch (Exception e)
{
// do not fail if we cannot patch the settings.json file
UnityEngine.Debug.LogException(e);
}
}

Expand Down Expand Up @@ -506,7 +540,7 @@ public override bool Open(string path, int line, int column, string solution)
var directory = IOPath.GetDirectoryName(solution);
var application = Path;

ProcessRunner.Start(string.IsNullOrEmpty(path) ?
ProcessRunner.Start(string.IsNullOrEmpty(path) ?
ProcessStartInfoFor(application, $"\"{directory}\"") :
ProcessStartInfoFor(application, $"\"{directory}\" -g \"{path}\":{line}:{column}"));

Expand Down
Loading