Skip to content
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

Append license comment rule #122

Closed
jeparlefrancais opened this issue Aug 17, 2023 · 5 comments · Fixed by #141
Closed

Append license comment rule #122

jeparlefrancais opened this issue Aug 17, 2023 · 5 comments · Fixed by #141
Labels

Comments

@jeparlefrancais
Copy link
Contributor

Create a rule that can append a given license at the beginning of each processed file. Something like:

{
  rule: "append_text_comment",
  // either
  text: "text to append",
  // or
  file: "LICENSE.txt"
}
@bainchild
Copy link

bainchild commented Aug 30, 2023

Maybe generalize this to any text, including lua(u) code? Prepending code could be used to do setup/environment checks, although it would make the rule to inject globals redundant (except for clarity). Also to specify if the code is being processed, a parameter for when to prepend the file (before/after processing). Not sure if this widens the scope of darklua too much, following the unix philosophy...

@jeparlefrancais
Copy link
Contributor Author

I'm not against a rule that could prefix files with some Lua code, but I'd make it a separate rule from this one. For security concerns, I think it's preferable to force the content to be commented out in case it's coming from something dynamic.

Also, I'm curious if you have a specific use case behind this, as it may be a good idea for a more specific rule. The use case of globals injection is a good example of that

@bainchild
Copy link

bainchild commented Sep 2, 2023

I think separating comments and code is a good idea. My specific use case is to generalize environment-specific functions, & otherwise check for capabilities. ex:

-- generalizing environment functions
local http_get
if env=="Roblox" then
   http_get=function(url) return game:GetService("HttpService"):GetAsync(url) end
elseif env=="ComputerCraft" then
   http_get=function(url)
      local handle = http.get(url)
      local content = handle.readAll()
      handle.close()
      return content
   end
--elseif env==...
end
if http_get then
   print(http_get('https://example.com'))
else
   error('No http function!')
end

-- optional environment setup
if INTERPRETER and type(INTERPRETER)=="table" and rawget(INTERPRETER,"load") then
   pcall(INTERPRETER.load,INTERPRETER,"metasafety")
end
--> work with objects safely, by telling the
--interpreter to use rawget, rawset, and typechecks
--to ensure that you don't trigger any metamethods
--without an error. For security reasons. 

I think my use case (execution in uncertain environments) is too specific for a rule that is worth the complexity for more than like, 5 people. Also once you have to do something like this, simple rule ordering might not be enough, and you may prefer a full preprocess stage.

@jeparlefrancais
Copy link
Contributor Author

What I would recommend for a use-case like this is to encapsulate the environment specific functions into their own files. For example, for a project where I want to support Lune and Roblox, I created a json.lua file to handle the environment specific functions.

if _G.ENV == "roblox" then
    local HttpService = game:GetService('HttpService') :: HttpService

    local function encode(value: unknown)
        return HttpService:JSONEncode(value)
    end

    return {
        encode = encode,
    }
end

local net = require('@lune/net')

local function encode(value: unknown)
    return net.jsonEncode(value)
end

return {
    encode = encode,
}

Then that way, I don't have to branch in all the places where I need this. I can just require my own interface and it's good to go. It is also pretty easy to extend and support more platforms if needed!

@bainchild
Copy link

It was only in 1 file for the example. In practice I do separate environment-specific functions into different files, then insert them into 1 file using a preprocess stage in the right branch. I think all the uses of the prepending code version of this rule could be solved by inlining/partial imports, which I feel are out of scope for darklua.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants