Search Unity

How to add a Scoped Registry using code?

Discussion in 'Package Manager' started by gillemp, Mar 18, 2021.

  1. gillemp

    gillemp

    Joined:
    Nov 23, 2015
    Posts:
    81
    I am trying to create an easy way to import this package easily in future projects.

    It needs to add a Scoped Registry before adding the dependency, and I was wondering how to do it all through code.

    Adding the dependency "should be easy" simply by using
    Code (CSharp):
    1. Client.Add("com.av.smart-hierarchy");
    But I cannot find anywhere how to add a Scope Registry... Does anybody know how to do it?

    Thanks in advance for your help and time!
     
  2. UnityMaru

    UnityMaru

    Community Engagement Manager PSM

    Joined:
    Mar 16, 2016
    Posts:
    1,227
    Hey there!

    Apologies for the delay in reply. I checked with the team who advise me that this replies on public API calls that is not supported at present, so it will likely not work.
     
    gillemp likes this.
  3. leon-sunday

    leon-sunday

    Joined:
    May 19, 2020
    Posts:
    12
    I'm facing exactly this issue at the moment
     
  4. firstuser

    firstuser

    Joined:
    May 5, 2016
    Posts:
    147
    Last edited: Jun 30, 2021
    tokar_dev likes this.
  5. bobbaluba

    bobbaluba

    Joined:
    Feb 27, 2013
    Posts:
    81
    I had to do this at work and ended up using reflection to call the private unity apis.

    There is a Client.AddScopedRegistry if I remember correctly.
     
    leon-sunday likes this.
  6. leon-sunday

    leon-sunday

    Joined:
    May 19, 2020
    Posts:
    12
    I ended with a similar solution than @firstuser but this Reflection approach sounds very interesting
     
  7. anschn_dmg

    anschn_dmg

    Joined:
    Sep 1, 2020
    Posts:
    3
    Is there a solutions to this, like an API in the Editor?
     
  8. basti_justdice

    basti_justdice

    Joined:
    Dec 9, 2019
    Posts:
    2
    If you add NewtonsoftJson to your project (available via PackageManager) following code lets you modify the `manifest.json` that contains the scoped registry:

    Code (CSharp):
    1.  
    2.     public static void AddMyScopedRegistry() {
    3.         AddScopedRegistry(new ScopedRegistry {
    4.             name = "Test Registry",
    5.             url = "https://my.scoped.registry",
    6.             scopes = new string[] {
    7.                 "my.scope"
    8.             }
    9.         });
    10.     }
    11.  
    12.     public static void AddScopedRegistry(ScopedRegistry pScopeRegistry) {
    13.         var manifestPath = Path.Combine(Application.dataPath, "..", "Packages/manifest.json");
    14.         var manifestJson = File.ReadAllText(manifestPath);
    15.  
    16.         var manifest = JsonConvert.DeserializeObject<ManifestJson>(manifestJson);
    17.  
    18.         manifest.scopedRegistries.Add(pScopeRegistry);
    19.  
    20.         File.WriteAllText(manifestPath, JsonConvert.SerializeObject(manifest, Formatting.Indented));
    21.     }
    22.  
    23.  
    24.     public class ScopedRegistry {
    25.         public string name;
    26.         public string url;
    27.         public string[] scopes;
    28.     }
    29.  
    30.     public class ManifestJson {
    31.         public Dictionary<string,string> dependencies = new Dictionary<string, string>();
    32.  
    33.         public List<ScopedRegistry> scopedRegistries = new List<ScopedRegistry>();
    34.     }
     
    CodeSmile and Basamas like this.