R/version_log.R

##---------------------------------------------------------------------------
##	PACKAGED
##---------------------------------------------------------------------------
packaged = function(x, user = Sys.getenv("USERNAME"))
{
	if(missing(x)) x = getwd()

	fd = file.path(x, "DESCRIPTION")

	if(!file.exists(fd)) stop("file DESCRIPTION not found", call.=F)

	tx1 = readLines(fd)
	n = grep("Packaged:", tx1)

	if(!length(n)) 
	{
		vd = "Packaged:" 
		#write(vd, file = fd, append = TRUE)
		tx1 = c(tx1, "Packaged:")
			
	} else {
		vd = tx1[n]
	}#end if

	ymdh = Sys.time()

	out <- format(ymdh, format="%Y-%m-%d %H:%M:%OS3 %Z", tz="UTC")
	dh = paste0(out, "; ", user) 

	nv = paste("Packaged:", dh)
	tx2 = gsub(pattern = vd, replacement = nv, x = tx1)
	writeLines(tx2, con = fd)

	return(dh)
}#end function packaged

##---------------------------------------------------------------------------
## RELEASE_DATE
##---------------------------------------------------------------------------
release_date = function(x)
{
	if(missing(x)) x = getwd()

	fd = file.path(x, "DESCRIPTION")

	if(!file.exists(fd)) stop("file DESCRIPTION not found", call.=F)

	tx1 = readLines(fd)
	n = grep("Date:", tx1)

	if(!length(n)) 
	{
		vd = "Date:" 
		#write(vd, file = fd, append = TRUE)
		tx1 = c(tx1, "Date:")
			
	} else {
		vd = tx1[n]
	}#end if

	ymdh = Sys.time()

	dh = format(ymdh, format="%Y-%m-%d")
	nv = paste("Date:", dh)
	tx2 = gsub(pattern = vd, replacement = nv, x = tx1)
	writeLines(tx2, con = fd)

	return(dh)
}#end function release_date


##---------------------------------------------------------------------------
## .VERSION_DATE
##---------------------------------------------------------------------------
.version_date = function(vd = "0.0.0", step = 1)
{
	vd = gsub("Version: ", "", vd)

	vd = gsub("-", "\\.", vd)

	w2 = unlist(strsplit(vd, "[.]"))

	if(length(w2) > 3 | length(w2) < 3) w2 = w2[1:3] 

	w2[is.na(w2)] = 0

	mv = as.numeric(w2)

	n1 = mv[1]
	n2 = mv[2]
	n3 = mv[3] + step

	if(n3 > 9)
	{
		n3 = "0"
		n2 = mv[2] + 1
	}#end if

	ymdh = Sys.time()

	out = format(ymdh, format="%Y-%m-%d %H:%M:%S")

	vp = gsub("-| |:", "", out)

	vn = paste(n1, ".", n2, ".", n3, ".", vp, sep="", collpase="")

	return(vn)

}#end function .version_date

##---------------------------------------------------------------------------
## .VERSION_DEV 
##---------------------------------------------------------------------------
.version_dev = function(vd = "0.0.0.001", step = 1)
{
	vd = gsub("Version: ", "", vd)

	vd = gsub("-", "\\.", vd)

	w2 = unlist(strsplit(vd, "[.]"))

	if(length(w2) < 4 | length(w2) > 4)
	{
		w2 = w2[1:4]
		w2[4] =  "000" 
	}#end if

	w2[is.na(w2)] = 0

	mv = as.numeric(w2)

	n4 = mv[4] + step
	 
	if(n4 > 999)
	{
		n4 = "000" 
		c3 = mv[3] + 1

		if(c3 > 9)
		{ 
			n3 = "0" 
			c2 = mv[2] + 1

		} else {

			c2 = mv[2]
			n3 = as.character(mv[3] + 1)
		}#end if

		if(c2 > 9)
		{
			n2 = "0"
			n1 = as.character(mv[1] + 1)

		} else {

			n1 = as.character(mv[1])
			n2 = as.character(c2)
		}#end if

		vn = paste(n1, ".", n2, ".", n3, ".", n4, sep="", collpase="")

	} else {

		n1 = as.character(mv[1])
		n2 = as.character(mv[2])
		n3 = as.character(mv[3])
		n4 = sprintf("%03d", mv[4] + step)

		vn = paste(n1, ".", n2, ".", n3, ".", n4, sep="", collpase="")
	}#end if

	return(vn)

}#end function version_dev


##---------------------------------------------------------------------------
## .VERSION_MAJOR
##---------------------------------------------------------------------------
.version_major = function(vd = "1.0.0", step = 1)
{
	vd = gsub("Version: ", "", vd)

	vd = gsub("-", "\\.", vd)

	w2 = unlist(strsplit(vd, "[.]"))

	if(length(w2) > 3 | length(w2) < 3) w2 = w2[1:3] 

	w2[is.na(w2)] = 0

	mv = as.numeric(w2)

	n1 = mv[1] + step
	n2 = mv[2]
	n3 = mv[3]

	vn = paste(n1, ".", n2, ".", n3, sep="", collpase="")

	return(vn)
}#end function .version_major

##---------------------------------------------------------------------------
## .VERSION_MINOR
##---------------------------------------------------------------------------
.version_minor = function(vd = "0.1.0", step = 1)
{
	vd = gsub("Version: ", "", vd)

	vd = gsub("-", "\\.", vd)

	w2 = unlist(strsplit(vd, "[.]"))

	if(length(w2) > 3 | length(w2) < 3) w2 = w2[1:3] 

	w2[is.na(w2)] = 0

	mv = as.numeric(w2)

	n1 = mv[1]
	n2 = mv[2] + step
	n3 = mv[3]

	if(n2 > 9)
	{
		n2 = "0"
		n1 = mv[1] + 1
	}#end if

	vn = paste(n1, ".", n2, ".", n3, sep="", collpase="")

	return(vn)
}#end function .version_minor

##---------------------------------------------------------------------------
## .VERSION_PATCH
##---------------------------------------------------------------------------
.version_patch = function(vd = "0.0.1", step = 1)
{
	vd = gsub("Version: ", "", vd)

	vd = gsub("-", "\\.", vd)

	w2 = unlist(strsplit(vd, "[.]"))

	if(length(w2) > 3 | length(w2) < 3) w2 = w2[1:3] 

	w2[is.na(w2)] = 0

	mv = as.numeric(w2)

	n1 = mv[1]
	n2 = mv[2]
	n3 = mv[3] + step

	if(n3 > 9)
	{
		n3 = "0"
		n2 = mv[2] + 1
	}#end if

	vn = paste(n1, ".", n2, ".", n3, sep="", collpase="")

	return(vn)
}#end function .version_patch

##---------------------------------------------------------------------------
## VERSION_LOG
##---------------------------------------------------------------------------
version_log = function(path, 
					  comment = NULL,
					  file.name = "DESCRIPTION", 
					  type = c("dev", "major", "minor", "patch", "vdate"),
					  num = NULL,
					  edit = TRUE,
					  count = TRUE,
					  step = 1)
{
	fd = file.path(path, file.name)

	if(!file.exists(fd)) 
	{
		if(is.null(num) & is.null(comment)) 
		{
			msg = "# Generated by me\nInicial version\n0.0.0.900"
			writeLines(msg, fd)
			return(msg)
		}#end if 
		if(!is.null(num) & !is.null(comment))
		{
			msg = paste0("# Generated by me\n", comment, "\n", num)
			writeLines(msg, fd)
			return(msg)
		}#end if
		if(!is.null(num) & is.null(comment))
		{
			msg = paste0("# Generated by me\nInicial version", "\n", num)
			writeLines(msg, fd)
			return(msg)
		}#end if
		if(is.null(num) & !is.null(comment))
		{
			msg = paste0("# Generated by me\n", comment, "\n0.0.0.900")
			writeLines(msg, fd)
			return(msg)
		}#end if
	}#end if

	tx1 = readLines(fd)

	if(edit){n = grep("Version", tx1)} else {n = length(tx1)}

	vd = tx1[n]

	if(!count & is.null(num)) return(gsub("Version: ", "", vd))

	if(is.null(num))
	{
		type = match.arg(type)
		switch(type,
			   dev  =  {nv <- .version_dev(vd, step)},
			   major = {nv <- .version_major(vd, step)},
			   minor = {nv <- .version_minor(vd, step)},
			   patch = {nv <- .version_patch(vd, step)},
			   vdate = {nv <- .version_date(vd, step)})

		if(edit){nv = paste("Version:", nv)}

	} else {
		if(edit)
		{
			nv = paste("Version:", num)
		} else {
			nv = tx1[n] = num
		}#end if
	}#end if

	if(!edit)
	{
		if(is.null(comment))
		{
			tx1[n+1] = nv
		} else {
			tx1[n+1] = paste0(comment,"\n", nv)
		}#end if
	}#end if

	if(edit)
	{
		tx2 = gsub(pattern = vd, replacement = nv, x = tx1)
		writeLines(tx2, con = fd)
	} else {
		writeLines(tx1, con = fd)
	}#end if

	return(gsub("Version: ", "", nv))
}#end function version_log


##-------------------------------------------------------------
## 
##-------------------------------------------------------------
version_news = function(path, txt = "")
{
    n = length(txt)

    if(n > 1) txt = c(txt[1], paste0("\t", txt[2:n]), "\n")

    fConn <- file(file.path(path, "NEWS"), 'r+')
    Lines <- readLines(fConn)
    writeLines(c(txt, Lines), con = fConn)
    close(fConn)
}#end version_news
salah31416/toolbox documentation built on June 3, 2019, 6:59 p.m.