dime-worldbank/dimeanalytics

Stata command: test that user-written commands are installed and updated

luizaandrade opened this issue · 5 comments

Stata command: test that user-written commands are installed and updated

I'm currently using these lines to check if the packages that I need are installed or updated:

*** Install required packages	
	local packages tabout ietoolkit winsor estout _gclsst 
		
	foreach pgks in `packages' {	
	  				
		capture which `pgks'
		
		if (_rc != 111) {
			display as text in smcl "Package {it:`pgks'} is installed"
			
			capture ado update `pgks'
			if (r(pkglist)) {
				display as text in smcl `"Package {it:`pgks'} is up to date."'
			}
		}
		
		else {
			display as error in smcl `"Package {it:`pgks'} needs to be installed from SSC in order to run this do-file;"' _newline ///
			`"This package will be automatically installed if it is found at SSC."'
			
			capture ssc install `pgks', replace
			
			if (_rc == 601) {
				display as error in smcl `"Package `pgks' is not found at SSC;"' _newline ///
				`"Please check if {it:`pgks'} is spelled correctly and whether `pgks' is indeed a user-written command."'
			}
			
			else {
				display as result in smcl `"Package `pgks' has been installed successfully"'
			}
		}
	}

Quite bulky, but I'll try to come with a dummy program for this (similar to what pacman does in R).

This looks good. One thing that I am not sure how to deal with is version. Checking that it is the last version is neat, but not necessary exactly what you want all the time. Sometimes a replication package brake because the version of the package you have installed is too new. I think this command should include some feature to help with that, but not sure a perfect solution is possible.

Agree. I tried it to look at the version installed using which and whether this can be saved into a scalar and compare it to the most updated version uploaded to SSC, but so far which doesn't return it, and didn't spend more time looking into it. I just left it to check the last version and update it if available. But you are right, when it comes to replication sometimes the most updated could break the code.

Here is what I tried

*! version 0.0.1  25aug2020

	capture program drop packages 

	program define packages 

		syntax [anything] [, update]
		
		foreach pgks of local anything {
								
			capture which `pgks'
			
			if (_rc != 111) {
			
				display as result in smcl "Package {it:`pgks'} is installed"
			}
			
			// Look if the package is installed			
			else {
				
				display as error in smcl `"Package {it:`pgks'} needs to be installed from SSC in order to run this do-file;"' _newline ///
				`"This package will be automatically installed if it is found at SSC."'
				
				capture ssc install `pgks', replace
				
				if (_rc == 601) {
					display as error in smcl `"Package `pgks' is not found at SSC;"' _newline ///
					`"Please check if {it:`pgks'} is spelled correctly and whether `pgks' is indeed a user-written command."'
					
					exit
				}
				
				else {
					display as result in smcl `"Package `pgks' has been installed successfully"'
				}
			}
					
			// With the update option
			if ("`update'" != "") {
				
				capture ado update `pgks'
				
				if (r(pkglist)) {
					display as result in smcl `"Package {it:`pgks'} is up to date."'
				}
				
			}
		}
		
	end 

The syntax is quite straightforward. It has only the option update; so, for example, it can run something in this fashion:
packages esttab
packages esttab, update

And it also accepts multiple pkgs at the same:
packages esttab outreg2 asdoc

Maybe this could be of use. Let me know what you think. I still need to add returned results and maybe other options.

I still think we need to find some way to make sure that the correct version of the package is installed, but just the most recent version.

Nothing in your code is incorrect, but I am concerned that this will be seen as using this command and all your package version control issues are fixed. Which is more than we can promise.