InverseTampere / TreeQSM

Quantitative Structure Models of Single Trees from Laser Scanner Data
Other
147 stars 46 forks source link

Change folder structure to use MATLAB packages #20

Open SeSodesa opened 1 year ago

SeSodesa commented 1 year ago

The folks at MathWorks have added the concept of packages into MATLAB, in the release R2019b. This makes it possible to access functions and classes either via fully qualified names

package.subpackage.{functionName,ClassName}

or by importing a function or a class at the start of a function. This means one does not need to pollute their MATLAB path in order to use code from a Git repository.

I propose that in a future release, the folder structure of TreeQSM be transformed to use MATLAB packages, as that would make it possible to set the project as a Git submodule in another MATLAB project, and access the functionality therein, again without polluting the MATLAB path. This would mean renaming each src/folder/subfolder/+folder/+subfolder/ in the project root.

With this, one could do

git submodule add <TreeQSM HTTPS URL> +TreeQSM

in their own project and then access a function fn in MATLAB with

results = TreeQSM.folder.subfolder.fn ( args, kwargs ) ;

This would be very helpful. The downside is that each existing function in TreeQSM would also need to be modified to either import or use the qualified names from the new package structure.

SeSodesa commented 1 year ago

If the name of the folder src/ has been changed to +TreeQSM and its subfolder names had been prefixed with + symbols, the following fish shell script might do most of the renaming automatically:

#!/usr/local/bin/fish
set mpaths (find +TreeQSM -name "*.m")
set noextpaths (path change-extension '' $mpaths)
set mdirs (path dirname $mpaths)
set fnames (basename $noextpaths)
for mpath in $mpaths
    echo
    echo $mpath
    echo
    for ii in (seq (count $fnames))
        set fname $fnames[$ii]
        set mdir $mdirs[$ii]
        set dirparts (string split '/' $mdir)
        set dirpartswithoutplus (string replace '+' '' $dirparts)
        set packageprefix (string join '.' $dirpartswithoutplus)
        if ggrep -qP "$fname" $mpath
            echo "  $fname → $packageprefix.$fname"
            gsed -Ei "s/$fname\(/$packageprefix.$fname\(/g" $mpath
        end
    end
end

The problem is that the function names at the start of the function files themselves also get changed to the following:

function out = TreeQSM.subpackage.functionname(args, kwargs)
    ...
end

These are much easier to change back to just the function names themselves, than it is to look for function names that need prefixing with TreeQSM.submodule. in the midst of other code. Also the command gsed (GNU sed) should be changed to just sed, if on Linux, where GNU sed is the default.