diff --git a/Docs/GenerateImplementation.md b/Docs/GenerateImplementation.md new file mode 100644 index 0000000..0ea644c --- /dev/null +++ b/Docs/GenerateImplementation.md @@ -0,0 +1,116 @@ +# Generate Module +P2PRC is a great layer of abstraction. This means that in many cases it is not an end product but rather +a tool that customized as an end product. An example would be writing your own billing module to monetize +the computation power available. The generate module copies the current with the appropriate git histories +and keeps only the go files which would be useful to edit. To use the generate module the user will need +to have a go compiler present in his computer. Due to the introduction of this module there will 2 releases: + +- Regular Release (Consists of only the build binary and cli command cannot access the generate module) +- Developer Release (Consists of important Go files and the cli can access the generate module) + +## How does this work ? + +### [Struct information](https://github.com/Akilan1999/p2p-rendering-computation/blob/9d69aed8ce0fe5273aaff2828f7d51c3d5ac2ce4/generate/generate.go#L19) +- ###```Generate.go```: +This file creates a local copy of P2PRC from where the CLI was called from. +This go file also does various stuff like instruction of file should be ignored when copying and +which of should not be. Now let's understand this. Below is a sample code which does the following: + +```go + //---------------------------------------------------------------- + // Action performed: + // - Ensuring main.go file exists + // - Skipping all .go files apart from the ones listed above + // - Skipping .idea/ directory + // - Skipping Makefile file + //---------------------------------------------------------------- + Options.Skip = func(src string) (bool, error) { + switch { + case strings.HasSuffix(src, "main.go"): + return false, nil + case strings.HasSuffix(src, ".go"): + return true, nil + case strings.HasSuffix(src, ".idea"): + return true, nil + case strings.HasSuffix(src, "Makefile"): + return true, nil + default: + return false, nil + } + } + + // Doing the copy + err = copy.Copy("", "", Options) +``` + +Unfortunately currently this will have to be manually edited in the ```Generate.go``` file. When using the generate +module the user also creates their own Go module which is the modified version of P2PRC. This means +if the 1 modified package is using another modified package then the appropriate import have to be modified +in the file where the import is called: + +Ex: +```go + //Sample Project module name = Test + //Package names: + //- Test/Genius + //- Test/GeGeGenuis + // + // When we call the generate function with the new project with the module name = MicDrop + // The new package name would be: + // - MicDrop/Genius + // - MicDrop/GeGeGenuis + + // Test/Genius code depends on the package Test/GeGeGenuis + import ( + "Test/GeGeGenuis" + ) + +// When we create a new module with the copy of the +// existing project we need change: +import ( + "MicDrop/GeGeGenuis" +) +``` + +To do this we have built functions which can modify import names in the Go file provided. +To customize the use case of your generate module you would need to manually add your own +imports which are supposed to be replaced and in which files they are supposed to be replaced +in. + +```go +// 1.0 - Test/Genius.go -> GeGeGenuis module +// a is struct of type NewProject + a.FileNameAST = "/Test/Genius.go" + // Get AST information of the file + err := a.GetASTGoFile() + if err != nil { + return err + } + // Change the appropriate Go file + err = a.ChangeImports("Test/GeGeGenuis", "MicDrop/GeGeGenuis") + if err != nil { + return err + } + // Writes the change to the appropriate file + err = a.WriteGoAst() + if err != nil { + return err + } +``` + +Higher order of execution of ```Generate.go```: +1. Copy entire P2PRC project and ignores files which are not meant to be copied +2. The folder name will be based on the new project name and the module name based on the new + module name provided. +3. Modifies the appropriate imports in the project as instructed in the code. +4. Creates a commit with the new changes in the new project. + + +- ### ``` modifyGenerate.go```: + This a really simple implementation where we replace the imports + in certain files as instructed from ```generate.go```. To do we create an AST (i.e Abstract Syntax tree) + from new file we want to change the imports in. AST create a tree structure of expression. To change the + import we can just traverse to the appropriate expression and change the value of that expression in + the case of modifying imports. This approach is more simple than using templates. + + diff --git a/Docs/Implementation.md b/Docs/Implementation.md index 619385c..9a45341 100644 --- a/Docs/Implementation.md +++ b/Docs/Implementation.md @@ -16,4 +16,5 @@ the project is git.sr.ht/~akilan1999/p2p-rendering-computation. ## [Server Module](ServerImplementation.md) ## [Client Module](ClientImplementation.md) ## [P2P Module](P2PImplementation.md) -## [Plugin Module](PluginImplementation.md) \ No newline at end of file +## [Plugin Module](PluginImplementation.md) +## [Generate Module](GenerateImplementation.md) \ No newline at end of file diff --git a/Docs/Installation.md b/Docs/Installation.md index 56aec37..436ebd7 100644 --- a/Docs/Installation.md +++ b/Docs/Installation.md @@ -176,6 +176,7 @@ p2prc --rmgroup ``` p2prc --gen --mod ``` +[read more about the generate module](GenerateImplementation.md) diff --git a/Docs/README.md b/Docs/README.md index 26b4b20..e7b0f9b 100644 --- a/Docs/README.md +++ b/Docs/README.md @@ -13,5 +13,6 @@ 4. [Config Module](ConfigImplementation.md) 5. [Cli Module](CliImplementation.md) 6. [Plugin Module](PluginImplementation.md) -5. [Problems](Problems.md) + 7. [Generate Module](GenerateImplementation.md) +5. [Problems](https://github.com/Akilan1999/p2p-rendering-computation/issues)