0

This question may be too open-ended for Stack Overflow, but I'm getting nowhere on my own. I am looking for a way to use automate the use of designated Git submodules based on values in a text file. My company generates a lot of projects, but we reuse a lot of code. We use submodules and tags for this, but the volume is starting to get unwieldy.

I was thinking it would make sense to use something similar to an NPM packages.json file. Maybe like this:

{
    "name": "Custom Development Components",
    "description": "Reusable objects to increase the efficiency and quality of custom development",
    "components": [
                    {
                        "name": "stats",
                        "description": "Statistics",
                        "URL": "https://my.git.repo/Statistics",
                        "folder-name": "STATS",
                        "version": "^"
                    },
                    {
                        "name": "standard",
                        "description": "Standard",
                        "URL": "https://my.git.repo/Standard",
                        "folder-name": "STANDARD",
                        "version": "X"
                    },
                    {
                        "name": "helpers",
                        "description": "Helper Objects and Scripts",
                        "URL": "https://my.git.repo/Helpers",
                        "folder-name": "HELPERS",
                        "version": "^"
                    }
    ]
}

But I haven't been able to figure out how to parse the JSON and turn it into Git commands. I've fiddled around with using JQ in a Windows batch file (we're all working in Windows environments), but I haven't gotten very far with it. Basically, I'm looking to be able to step through the JSON, find components where the version is a string other than ^ (which may or may not be a specific tag), and import the URL for the submodule.

Any guidance here would be appreciated.

Dave Chapman
  • 276
  • 2
  • 12
  • 2
    Example command? – ikegami Nov 02 '21 at 16:26
  • If you just need the URLs as you seem to have stated, `.components[] | select( .version != "^" ) | .URL` is the program you want. (You can replace the quotes with single quotes.) With `-r`, this will produce a URL per line. But you need to build a shell command form that, and I think `^` might be special and need escaping. At least for `cmd`. I don't know much about PowerShell, but would easier to convert the URLs into command using PowerShell rather than `cmd`. – ikegami Nov 02 '21 at 16:28
  • So the outstanding questions to which you need to find an answer: 1) How to produce a shell command that produces this string for jq's argument. 2) [What would be an equivalent to `xargs` in PowerShell](https://stackoverflow.com/q/36428949/589924) (In `cmd`, you'd need to use `for`.) – ikegami Nov 02 '21 at 16:29

1 Answers1

0

just use something like this this:

$ jq -r < /tmp/o '.components[] | select( .version != "^" ) | "git submodule init \(.URL):\(.version)"'
git submodule init https://my.git.repo/Standard:X

I'm not much versed in the bat language, but you may have some luck with: