Skip to content

Conversation

@noresources
Copy link
Contributor

What does this PR do?

Adds new API requireopt() to require a module without throwing an error

How does this PR change Premake's behavior?

Also adds a third argument silent to the existing require() (default: false)

Anything else we should know?

Use case: Have a way to handle optional modules.
Example

local doxygen = requireopt("doxygen")
if doxygen then
  doxyfile {
    project_name = "..."
  }
end

Did you check all the boxes?

  • Focus on a single fix or feature; remove any unrelated formatting or code changes
  • Add unit tests showing fix or feature works; all tests pass
  • Mention any related issues (put closes #XXXX in comment to auto-close issue when PR is merged)
  • Follow our coding conventions
  • Minimize the number of commits
  • Align documentation to your changes

@@ -0,0 +1,31 @@
--
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be named "test_globals.lua" to match the name of the file we are testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File was named based on existing test named "tests/base/test_module_loader.lua"

return mod
end)

function requireopt(modname, versions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation? I'm not completely sold on "requireopt", as that feels like an oxymoron to me. What about "tryrequire"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. It was named requireopt in an effort to follow existing naming convention, based on the existing dofilepot.
IMO, both of them should be renamed try<thing>

noresources and others added 8 commits January 12, 2025 18:06
Add a thired argument to the Premake require(modname, version, silent) function
to disable error throwing when module failed to load.

Add requireopt(m, v) as a shorthand for require(m, v, true)
Co-authored-by: Joris Dauphin <[email protected]>
Co-authored-by: Joris Dauphin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants