Search code examples
parsingnim-langnimble-nim

How do I access parsing of .nimble files from the nimble package?


Nim as a language provides .nimble files to describe its packages (example of a .nimble file). I know that the file is parsed by the nimble package and CLI-tool, as they need the information inside the .nimble file for their tasks.

I want basically all the information in there, dependencies, author, license, description, version, all of it. So in order to not do the same work twice and potentially run into issues should the format change, I would like to use the nimble package itself to parse the .nimble file for me.

I know the correct proc for it, which is getPkgInfoFromFile, but I can't seem to access it with import nimble/nimblepkg/packageparser. Whenever I use that line I receive an error that there is no such file.

What am I doing wrong?

Further: getPkgInfoFromFile requires an Options instance that it generates when parsing a CLI command. I don't have a CLI command, so I'm not generating such an instance, can I use the proc somehow without one?


Solution

  • Thanks to ringabout I came to the correct solution, but first to the question.

    Question 1: How do I access the proc in the first place?

    You can access nimble like a package, but the import is not import nimble/nimblepkg/packageparser it is directly import nimblepkg/packageparser.

    This requires you to have both nimble' installed as a library as well as the compiler` installed as a library.

    So you'll have to install those first:

      nimble install nimble
      nimble install nim # Originally this was called "compiler", but was renamed to "nim"
    

    Ignore any warnings if they pop up.

    Now you can compile the following dummy-example:

    #dummy.nim
    import nimblepkg/packageparser
    
    echo "Pointer to packageparser proc: ", packageparser.getPkgInfoFromFile.repr
    

    with: nimble -d:ssl --mm:refc -r build (-d:ssl is required for nimble's HTTP-client and --mm:refc is required as nimble appears to not work with orc)

    Question 2: Can I run the getPkgInfoFromFile without an Options instance?

    Yes-ish. You still need one, but it doesn't have to be a "real" one, you can just instantiate one yourself on the fly.

    import nimblepkg/[options, packageinfotypes, packageparser]
    
    proc generateDummyOptions(): Options =
      result = initOptions()
      result.setNimBin()
      result.setNimbleDir()
    
    proc parseNimbleFile*(nimblePath: string): PackageInfo =
      let options = generateDummyOptions()
    
      result = getPkgInfoFromFile(nimblePath.NimbleFile, options)