You need to sign in or sign up before continuing.
rpmgs 28.5 KB
Newer Older
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1
#!/bin/bash
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2
# 2005-2006, 2009, 2014, 2015, 2016, 2017, 2020 (c) Etersoft www.etersoft.ru
3
# 2005-2016 Author: Vitaly Lipatov <lav@etersoft.ru>
4 5 6
# Public domain
#
# GS - get source
7
#
8
# Скачивает исходники, автоматически выправляя ситуацию с gz/bz2/tgz/zip (tar для git, tar.bz2 для src.rpm)
9 10
# Параметры:
# - название спек-файла
11 12 13
# -a - get all source
# check for the same file with other compression

14 15
# load common functions, compatible with local and installed script
. `dirname $0`/../share/eterbuild/functions/common
16
load_mod rpm tarball web buildsrpm
17 18 19

WEXT=""
GETSOURCE=""
20
LOADALL=''
21 22

#############################
Vitaly Lipatov's avatar
Vitaly Lipatov committed
23
Usage="Usage: $name [-f] [spec] [new_version]"
24 25 26
function mygetopts()
{
name=${0##*/}
27
Descr="$name (Get Source) - get sources by spec / repository"
28 29 30 31 32

phelp()
{
	echog "$Descr"
	echog "$Usage"
33
	echog "You can run 'rpmgs 1.2' for set new version 1.2 and download it"
34
	echog "Use HEAD instead version for get latest git commit"
35
	echo
36
	echog "Options:"
37
#	echog "   -a  get all source (not only Source|Source0)"
38
	echog "   -f  force download and commit tarball(s) (remove source file before download)"
39 40
}

41
while getopts :hf opt; do
42 43
    case $opt in
    h) phelp; exit 0;;
44
#    a) LOADALL=1 ;;
45
    f) FORCEDOWNLOAD=-f ;;
46 47 48 49 50 51
    +?) echog "$name: options should not be preceded by a '+'." 1>&2; exit 2;;
    ?)  echog "$name: $OPTARG: bad option.  Use -h for help." 1>&2 ; exit 2;;
    esac
done
 
# remove args that were options
52
[ "$OPTIND" -gt 0 ] && shift $(($OPTIND - 1))
53 54 55 56 57

LISTRPMARGS=$@

}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
58
repack_tarball()
59
{
60
	# TODO: move it into repack
Vitaly Lipatov's avatar
Vitaly Lipatov committed
61
	[ "$(realpath "$1")" = "$(realpath "$2")" ] && return
62 63 64 65 66 67
	# skip repack for the same ext
	if [ "$(get_ext "$1")" = "$(get_ext "$2")" ] ; then
		echog "The same ext $(get_ext "$1"), skip repack"
		mv -f "$1" "$2"
		return
	fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
68
	docmd erc -f repack "$1" "$2"
69 70
	# remove original
	rm -f "$1"
71 72
}

73 74 75 76 77 78
check_tarball()
{
	[ -s "$1" ] || return
	erc -q check "$1"
}

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
# Args: URL TARGET
git_to_tarball()
{
	local URL="$1"
	local TARGET="$2"
	local CHECKOUT=''

	info "Create tarball $(basename $TARGET) from $URL ..."

	# allow commit or version
	if echo "$URL" | grep -q "github\.com.*/tree/" ; then
		CHECKOUT="$(echo "$URL" | sed -e 's|.*/tree/||')"
		URL="$(echo "$URL" | sed -e 's|/tree/.*||')"
	fi

94 95 96 97 98 99
	# allow commit
	if echo "$URL" | grep -q "github\.com.*/commit/" ; then
		CHECKOUT="$(echo "$URL" | sed -e 's|.*/commit/||')"
		URL="$(echo "$URL" | sed -e 's|/commit/.*||')"
	fi

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	local d="$(basename "$URL" .git)"
	git clone --recurse-submodules "$URL" "$d" || fatal

	if [ -n "$CHECKOUT" ] ; then
		cd "$d" || fatal
		git checkout $CHECKOUT || fatal
		cd - >/dev/null
	fi

	rm -rf "$d/.git/"
	# note: TARGET can be various
	docmd erc pack "$TARGET" "$d" || fatal
	rm -rf "$d"
}

115 116 117 118 119 120
# Args: URL TARGET
download_to()
{
	local URL="$1"
	local TARGET="$2"
	pushd $(dirname $TARGET)
121
	# make tarball from remote git (with submodules)
122
	if echo "$URL" | grep -q "\.git$" || echo "$URL" | grep -q -E "github\.com.*/(tree|commit)/" ; then
123 124 125 126 127
		git_to_tarball "$URL" "$TARGET"
		local RET=$?
		popd
		return $RET
	fi
128 129 130 131
		local DF="$(basename "$URL")"
		download_url "$URL" && repack_tarball "$DF" "$TARGET"
		local RET=$?
		# TODO: repack need remove origin
132
		#rm -fv "$DF"
133 134 135 136 137
		[ "$RET" = "0" ] || rm -fv "$DF" "$TARGET"
	popd
	return $RET
}

138
# Args: URL target_file
139 140
download_any_tarball()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
141 142 143 144
	local GETSOURCE="$1"
	local TARGET="$2"
	local FORMATS="tar.xz tar.bz2 tar.gz zip tgz 7z tbz2 tbz rar tar"
	local BASESOURCE="$GETSOURCE"
145

146
	local ORIGEXT=$(get_ext "$BASESOURCE")
147
	[ -n "$ORIGEXT" ] || fatal "Error with $GETSOURCE. Have no idea how to load files without extension"
148 149 150 151 152 153

	# first try download with original extension (exclude for tar)
	if [ "$ORIGEXT" != "tar" ] ; then
		FORMATS="$ORIGEXT $(estrlist exclude "$ORIGEXT" "$FORMATS")"
	fi

154
	BASESOURCE=$(dirname "$BASESOURCE")/$(basename "$BASESOURCE" .$ORIGEXT)
155 156 157

	local ext
	# try download by exts list
Vitaly Lipatov's avatar
Vitaly Lipatov committed
158
	for ext in $FORMATS ; do
159
		[ -z "$FORCEDOWNLOAD" ] && check_tarball "$TARGET" && { echo "$TARGET already exists, continue... " ; continue; }
160
		download_to "$BASESOURCE.$ext" "$TARGET" || continue
Vitaly Lipatov's avatar
Vitaly Lipatov committed
161
		return
162 163
	done
	fatal "Cannot retrieve $GETSOURCE"
164 165
}

166 167 168 169 170 171
__replace_first_line()
{
	# https://unix.stackexchange.com/questions/250603/replace-only-on-the-first-matching-line-with-sed
	sed -e "1,/$1/s/$1/$2/"
}

172 173 174 175 176 177
# param: spec name number (f.i., url for Source-url)
function source_ext()
{
	local GETSOURCEEXT=
	# %define SourceUrl ftp://updates.etersoft.ru/pub/Etersoft/WINE@Etersoft/last/sources/tarball/%name-%version.tar.gz
	#GETSOURCEURL=$(eval_spec $1 | grep -i "^%define ${2}Url${3} " | head -n 1 | sed -e "s/ *\$//g" | sed -e "s/^%define[ \t].*[ \t]//g")
178
	local SN="$3"
179 180
	#[ "$SN" = "0" ] && SN="$SN\?"
	if grep -q "^# $SN-$2:" "$1" ; then
181
		local TMPSPEC=$1.tmpurl
182
		local NEWSOURCE=$(grep --text "^# $SN-$2:" "$1" | sed -e "s/.*$SN-$2:[ \t]*//g" | tail -n1)
183
		test -n "$NEWSOURCE" || fatal "Can't extract URL from $SN-$2"
184 185
		# Fake replace for correct subst variables
		NEWSOURCE="$(echo "$NEWSOURCE" | sed -e 's|\&|\\&|g')"
186
		# TODO: move to separate function and rewrite
Vitaly Lipatov's avatar
Vitaly Lipatov committed
187
		# TODO: use special field before %build
188
		cat $1 | __replace_first_line "^Summary:.*" "" | sed -e "s|^\(# $SN-$2:.*\)|Summary: $NEWSOURCE\n\1|" > $TMPSPEC
189
		# TODO: replace only first entry
190
		#cat $1 | sed -e "s|^Summary:.*|Summary: $NEWSOURCE|1" > $TMPSPEC
191
		GETSOURCEEXT=$(eval_spec "$TMPSPEC" | get_var "Summary")
192
		#rhas "$GETSOURCEEXT" "%" && fatal "some macro unexpanded in URL. Check $TMPSPEC"
193 194
		rm -f "$TMPSPEC"
	fi
195
	
196 197 198 199
	echo "$GETSOURCEEXT"
	test -n "$GETSOURCEEXT"
}

200
# Get real URL from comment Source-xxx
201 202 203 204 205 206 207 208 209

# Source-svn: http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/Collection/
function get_source_svn()
{
	GETSOURCESVN=$(source_ext "$1" svn "$2")
}


# Source-git: http://git.altlinux.org/people/lav/packages/rpm-build-fonts.git
210
function get_source_git()
211
{
212
	local SN="$2"
213 214 215 216

	GETSOURCEGIT=$(source_ext "$1" git "$2")
	[ -n "$GETSOURCEGIT" ] && return

217 218
	# hack for git url under Source
	local HGIT="$(grep -B1 "^$SN:" "$1" | head -n1 | grep "^# " | sed -e "s|#[[:space:]]*||" -e "s|[[:space:]]*$||")"
219 220
	if echo "$HGIT" | grep -q -E "(git|https|http)://.*\.git$" || echo "$HGIT" | grep -q "https://github.com/.*" ; then
		GETSOURCEGIT="$(echo "$HGIT" | sed -e "s|.*[[:space:]]\(.*://\)|\1|")" #"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
221 222
	else
		return 1
223
	fi
224
}
225

226 227 228 229 230 231
# VCS: http://git.altlinux.org/people/lav/packages/rpm-build-fonts.git
function get_source_vcs()
{
	local SPEC="$1"
	local SN="$2"

232 233 234
	# only for first source
	[ "$SN" = "Source" ] || return 1

235 236 237
	local vcs="$(grep "^VCS:" $SPEC | head -n1 | sed -e 's|VCS:[[:space:]]*||')"
	if [ -z "$vcs" ] ; then
		# try hack with Url:
238
		local vcs="$(grep -i "^Url:" $SPEC | head -n1 | sed -e 's|Url:[[:space:]]*||I')"
239 240 241 242 243 244 245
		[ -z "$vcs" ] && return 1
		GETSOURCEGIT="$vcs"
		echo "$vcs" | grep -q "\.git$" || GETSOURCEGIT="$vcs.git"
	fi
	GETSOURCEGIT="$vcs"
}

246 247 248 249
# Source-url: ftp://updates.etersoft.ru/pub/Etersoft/WINE@Etersoft/last/sources/tarball/%name-%version.tar.gz
function get_source_url()
{
	GETSOURCEURL=$(source_ext "$1" url "$2")
250
	#rhas "$GETSOURCEURL" "%" && fatal "some macro unexpanded in URL. Check $TMPSPEC"
251 252
}

253 254 255 256 257 258
# Source-script: .gear/update.sh
function get_source_script()
{
	GETSOURCESCRIPT=$(source_ext "$1" script "$2")
}

259 260
function print_error()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
261 262
	echog "Can't find any spec file. It is possible you run this script not in git dir."
	echog "If you use old style build, run rpmgs with spec name as arg."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
263
	echog "If you wish download src.rpm, use rpmgp script."
264 265 266
	exit 1
}

267 268 269
# tarball dirname [options]
gear_update_from_tarball()
{
270
	local CREATEFLAG=-f
271 272 273
	local TARBALL="$1"
	local CURNAME="$2"
	shift 2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
274
	assert_var TARBALL CURNAME
275 276
	[ -d "$CURNAME" ] || CREATEFLAG=-c
	# TODO: check tarball ext. for unsupported arch and realize it here or in gear-update
Vitaly Lipatov's avatar
Vitaly Lipatov committed
277
	echo "Commit tarball '$TARBALL' to git subdir '$CURNAME'..."
278
	if ! docmd gear-update $CREATEFLAG $@ "$TARBALL" "$CURNAME" ; then
279
		if a= gear-update $CREATEFLAG $@ "$TARBALL" "$CURNAME" 2>&1 | grep -q "More than one subdirectory specified" ; then
280
			info "Try unpack as is"
281
			CREATEFLAG="$CREATEFLAG -a"
282
			docmd gear-update $CREATEFLAG $@ "$TARBALL" "$CURNAME" && return
283
		fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
284 285
	else
		return 0
286 287
	fi
	fatal "can't import tarball '$TARBALL'"
288 289
}

290 291
# if we have named rule
is_predownloaded_rule()
292
{
293
	cat $(get_root_git_dir)/.gear/rules | grep -q "^tar:.*\.gear/$1"
294 295 296
	#fatal "missed tar:.gear/gear-sources in $(get_root_git_dir)/.gear/rules"
}

297

298
# Args: tarball dir
299
# Uses: RPMSOURCEDIR FORCEDOWNLOAD
300
commit_tarball_to_dir()
301
{
302
	local TARBALL="$1"
303 304 305 306 307 308 309
	local EXTTARBALL="$2"
	local CURNAME="$3"

	if [ "$EXTTARBALL" = "copy" ] ; then
		#local OLDFILE=$(echo "$CURNAME")
		cp -v "$TARBALL" "$CURNAME"
		docmd git add "$CURNAME"
310 311 312 313 314
		if [ -n "$CURRENTURL" ] ; then
			docmd git commit -m "just import $CURRENTURL to $(basename $CURNAME) subdir with rpmgs script"
		else
			docmd git commit -m "just import $(basename $CURNAME) with rpmgs script"
		fi
315 316
		return 0
	fi
317

318 319
	if [ -f "$TARBALL" ] ; then
		gear_update_from_tarball "$TARBALL" "$CURNAME" $FORCEDOWNLOAD || { warning "Error with update tarball in repo" ; return 1 ; }
320 321
		# force commit ever files from .gitignore
		docmd git add -f "$CURNAME"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
322
		if [ -s ./.gear/postdownload-hook ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
323
			info "Detected ./.gear/postdownload-hook, running it in $CURNAME dir ..."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
324
			cd $CURNAME || fatal
325
			sh -x ../.gear/postdownload-hook
Vitaly Lipatov's avatar
Vitaly Lipatov committed
326 327
			cd - >/dev/null
		fi
328 329 330 331 332
		if [ -n "$CURRENTURL" ] ; then
			docmd git commit -m "just import $CURRENTURL to $(basename $CURNAME) subdir with rpmgs script"
		else
			docmd git commit -m "just import $(basename $TARBALL) with rpmgs script"
		fi
333
		rm -fv "$TARBALL"
334
	else
335
		warning "Skip missed $TARBALL tarball committing"
336 337 338 339 340
		return 1
	fi
	return 0
}

341
# TODO: rewrite for any tarball commit
342 343
# Arg: tarball
commit_tarball()
344
{
345
	local TARBALL="$1"
346
	local EXTTARBALL="$(get_ext $TARBALL)"
347
	local CURNAME
348 349
	CURNAME=$(get_tardir_from_rules "$EXTTARBALL" $(basename "$TARBALL"))
	if [ -z "$CURNAME" ] ; then
350
		#info "Can't get dir (no $EXTTARBALL: line in rules file), just commit $(basename "$TARBALL") file"
351
		CURNAME=$(get_tardir_from_rules "copy" $(basename "$TARBALL"))
Vitaly Lipatov's avatar
Vitaly Lipatov committed
352
		[ -n "$CURNAME" ] || fatal "There is no correct '$EXTTARBALL:' line nor 'copy:' in gear rules file for $(basename "$TARBALL"), needed for commit tarball"
353
		EXTTARBALL="copy"
354
	fi
355
	echo "$CURNAME" | grep -q "[\*\?]" && fatal "$CURNAME for $TARBALL got incorrectly"
356
	# FIXME:
357
	# use real path for download
358
	#is_gear_sources && CURNAME=
359
	#is_gear_sources && CURNAME=$(get_root_git_dir)/$BASENAME
360
	#fatal "FIXME: fail with is_gear_sources in commit_tarball"
361

Vitaly Lipatov's avatar
Vitaly Lipatov committed
362
	# hack: try detect dir for unpacking
363 364
	#test -d "$CURNAME" || CURNAME=$(get_root_git_dir)/$(get_tarballname "$spec")
	#test -d "$CURNAME" || CURNAME=$(get_root_git_dir)/$BASENAME
Vitaly Lipatov's avatar
Vitaly Lipatov committed
365

366 367 368
	# save first committed source dir
	[ -z "$FIRSTSOURCEDIR" ] && FIRSTSOURCEDIR="$(basename "$CURNAME")"

369
	commit_tarball_to_dir "$TARBALL" "$EXTTARBALL" "$CURNAME"
370 371
}

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
# check for tag for GSSETVERSION with some heuristics (see gear-remotes-uscan also)
get_tag_by_version()
{
	local GSSETVERSION="$1"
	local rc
	rc=1

	if [ "$GSSETVERSION" = "HEAD" ] ; then
		echo "upstream/master"
		return 0
	fi

	local i
	alternate_tag1="$(echo "v$GSSETVERSION" | sed -e "s|\.|-|g")"
	alternate_tag2="$(echo "REL_$GSSETVERSION" | sed -e "s|\.|_|g")"
	alternate_tag3="$(echo "version_$GSSETVERSION")"
	for i in v$GSSETVERSION $alternate_tag1 $GSSETVERSION $alternate_tag2 $alternate_tag3 ; do
		git rev-parse $i >/dev/null 2>/dev/null && rc=0 && break
	done

	if [ "$rc" = "1" ] ; then
		i="$(git tag | grep "$GSSETVERSION\$" | head -n1)"
		[ -n "$i" ] && git rev-parse $i >/dev/null 2>/dev/null && rc=0
	fi
	echo "$i"
	return $rc
}

# TODO: try gear-remotes-uscan here
# merge from guessed tag (by version) or from HEAD
update_master_branch_to()
{
	local GSSETVERSION="$1"
	if [ -z "$GSSETVERSION" ] ; then
		warning "Empty new version variable"
		return
	fi

	local tag="$(get_tag_by_version "$GSSETVERSION")"
	[ -n "$tag" ] || fatal "Can't find tag for $GSSETVERSION version"
	docmd git merge $tag

	# TODO: it is more clean detect that dir
	if [ -d "$(get_root_git_dir)/.gear/tags" ] ; then
		docmd gear-update-tag -a
		cd $(get_root_git_dir)/.gear || fatal
		docmd git add tags/* -f
		docmd git commit -m "update .gear/tags"
		cd - >/dev/null
	fi
}

424 425 426 427
# update .gear/@name@-postsubmodules if needed
# uses: BASENAME VERSION
update_post_git_submodules()
{
428
	# TODO: check for rules
429 430 431 432 433
	local PSM=$(get_root_git_dir)/.gear/$BASENAME-postsubmodules
	[ -d $PSM ] || return

	info "Detected post git submodules hook"

434
	cat $(get_root_git_dir)/.gear/rules | grep -q "tar:.*-postsubmodules" || fatal "missed tar:.gear/$BASENAME-postsubmodules in $(get_root_git_dir)/.gear/rules"
435
	[ -s $(get_root_git_dir)/.gitmodules ] || fatal "there is no .gitmodules file in repo root"
436

437
	cd $(get_root_git_dir) || fatal
438
	docmd git submodule init || fatal
439
	docmd git submodule sync
440
	docmd git submodule update --recursive || fatal
441 442 443

	rm -rfv $PSM/*
	# echo * do not catch .names really
444 445 446 447 448
	#cp -a $(echo * | grep -v -E "(.gear|.git)") $PSM/ || fatal #"
	#find $PSM -name ".git" -type f -delete -print
	#find $PSM -name ".gitmodules" -type f -delete -print
	local i
	for i in $(grep "path = " .gitmodules | sed -e "s|.*path = \(.*\)|\1|") ; do #"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
449
		cp -al --parents $i $PSM/ || fatal
450
	done
451
	docmd git add -f $PSM
452 453
	# TODO: put short commit id in a description
	# git rev-parse --short HEAD
454 455 456
	docmd git commit $PSM -m "update source prepared with submodules for version $VERSION"
}

457 458 459 460
source_postupdate_hook()
{
	local RGD=$(get_root_git_dir)
	if [ -s $RGD/.gear/source-postupdate-hook ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
461
    		info "Detected .gear/source-postupdate-hook, running it ..."
462
		sh $RGD/.gear/source-postupdate-hook $VERSION
463 464 465
	fi
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
466
# FIXME: run only for predownload, not for every source
467 468
# param: development, production
update_predownloaded()
469
{
470 471 472 473
	local MODE="$1"
	local SDNAME=predownloaded-$MODE
	is_predownloaded_rule $SDNAME || return
	PSM=$(get_root_git_dir)/.gear/$SDNAME
474 475 476

	local RGD=$(get_root_git_dir)

477
	CURNAME=$FIRSTSOURCEDIR
478
	# hack for subdir??
479
	#CURNAME=$BASENAME
480

Vitaly Lipatov's avatar
Vitaly Lipatov committed
481
	info "Preparing sources in $PSM for mode $MODE ..."
482 483
	# TODO: add support for git
	rm -rf $PSM
484 485 486 487 488 489 490
	if [ -n "$GETSOURCEGIT" ] ; then
		# cp -a exclude .dir
		mkdir -p $PSM || fatal
		cp -a $RGD/* $PSM || fatal
	else
		cp -a $RGD/$CURNAME $PSM || fatal
	fi
491

492
	pushd $PSM || fatal
493

494 495 496
	local RUNHOOK="sh"
	[ -n "$VERBOSE" ] && RUNHOOK="sh -x"

497
	if [ -s $RGD/.gear/predownloaded-preinstall-hook ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
498
		echo
499 500
		info "Detected .gear/predownloaded-preinstall-hook, running it ..."
		$RUNHOOK $RGD/.gear/predownloaded-preinstall-hook $MODE $VERSION
501
                COMMITMSG="update predownloaded-$MODE with a hook script"
502 503
	fi

504

505 506 507 508 509 510 511
	#### composer only part
	if [ -s "./composer.json" ] ; then
		local COMMITMSG=''
                local PRODUCTION=''

		[ "$MODE" = "production" ] && PRODUCTION='--no-dev'

512 513
		info "Detected composer install hook, running ..."
		docmd composer install $PRODUCTION || fatal
514 515 516
		COMMITMSG="update php modules with composer install $PRODUCTION for $VERSION (see $SDNAME in .gear/rules)"

		if [ -s $RGD/.gear/predownloaded-postinstall-hook ] ; then
517 518
		        info "Detected .gear/predownloaded-postinstall-hook, running it ..."
			$RUNHOOK $RGD/.gear/predownloaded-postinstall-hook $MODE $VERSION
519
		fi
520

521 522 523 524 525
		info "Removing binaries ..."
		find vendor -name "*.a" -type f -delete -print
		find vendor -name "*.so" -type f -delete -print
		find vendor -name "*.dll" -type f -delete -print

526 527
		# drop all exclude vendor
		# TODO: correct .* removing
Vitaly Lipatov's avatar
Vitaly Lipatov committed
528
		rm -rf $(ls -1 | grep -v vendor) .[a-zA-Z0-9]*
529 530 531
	fi
	#### end of composer only part

532

533 534 535 536 537 538 539
	#### go only part
	if [ -s "./go.mod" ] && [ ! -d "./vendor" ] ; then
		local COMMITMSG=''
		local PRODUCTION=''

		#[ "$MODE" = "production" ] && PRODUCTION='--no-dev'

540 541
		info "Detected go.mod install hook, running ..."
		docmd go mod vendor $PRODUCTION || fatal
542 543 544 545 546 547 548
		COMMITMSG="update vendored go modules with go mod vendor $PRODUCTION for $VERSION (see $SDNAME in .gear/rules)"

		if [ -s $RGD/.gear/predownloaded-postinstall-hook ] ; then
		        info "Detected .gear/predownloaded-postinstall-hook, running it ..."
			$RUNHOOK $RGD/.gear/predownloaded-postinstall-hook $MODE $VERSION
		fi

549 550 551 552 553
		info "Removing binaries ..."
		find vendor -name "*.a" -type f -delete -print
		find vendor -name "*.so" -type f -delete -print
		find vendor -name "*.dll" -type f -delete -print

554 555 556 557 558 559
		# drop all exclude vendor
		# TODO: correct .* removing
		rm -rf $(ls -1 | grep -v vendor) .[a-zA-Z0-9]*
	fi
	#### end of composer only part

560 561 562 563 564 565 566 567 568

	#### cargo only part
	if [ -s "./Cargo.lock" ] && [ ! -d "./vendor" ] ; then
		local COMMITMSG=''
		local PRODUCTION=''

		#[ "$MODE" = "production" ] && PRODUCTION='--no-dev'

		info "Detected 'Cargo.lock' install hook ..."
569
		docmd cargo vendor --verbose $PRODUCTION || fatal
570 571 572 573 574 575 576
		COMMITMSG="update vendored cargo modules with cargo vendor $PRODUCTION for $VERSION (see $SDNAME in .gear/rules)"

		if [ -s $RGD/.gear/predownloaded-postinstall-hook ] ; then
		        info "Detected .gear/predownloaded-postinstall-hook, running it ..."
			$RUNHOOK $RGD/.gear/predownloaded-postinstall-hook $MODE $VERSION
		fi

577 578 579 580 581 582 583 584
		# TODO: can't just remove binaries (it is checked)
		info "Check news about windows target: https://github.com/rust-lang/cargo/issues/6179"
		#find vendor -name "*.a" -type f -delete -print
		#find vendor -name "*.so" -type f -delete -print
		#find vendor -name "*.dll" -type f -delete -print

		# can remove binaries from definitely unused packages
		rm -rv vendor/winapi-*-pc-windows-gnu/lib/*.a
585 586 587 588 589 590

		# drop all exclude vendor
		# TODO: correct .* removing
		# TODO: move from temp dir
		rm -rf $(ls -1 | grep -v vendor | grep -v Cargo.lock) .[a-zA-Z0-9]* *.md
	fi
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620

	local i
	local rust_dir=''
	for i in */Cargo.lock ; do
		[ -s "$i" ] || continue
		rust_dir=$(dirname $i)
		[ -d "./vendor" ] && continue

		local COMMITMSG=''

		info "Detected '$i' install hook ..."
		cd $rust_dir || fatal
		docmd cargo vendor --verbose ../vendor || fatal
		COMMITMSG="update vendored cargo modules in $rust_dir dir with cargo vendor for $VERSION (see $SDNAME in .gear/rules)"

		if [ -s $RGD/.gear/predownloaded-postinstall-hook ] ; then
		        info "Detected .gear/predownloaded-postinstall-hook, running it ..."
			$RUNHOOK $RGD/.gear/predownloaded-postinstall-hook $MODE $VERSION
		fi

		cd - >/dev/null

		# can remove binaries from definitely unused packages
		rm -rv vendor/winapi-*-pc-windows-gnu/lib/*.a

		# drop all exclude vendor
		# TODO: correct .* removing
		# TODO: move from temp dir
		rm -rf $(ls -1 | grep -v vendor | grep -v Cargo.lock) .[a-zA-Z0-9]* *.md
	done
621 622 623
	#### end of cargo only part


624 625 626 627 628 629 630
	#### npm only part
	if [ -s "./package.json" ] ; then

		# CHECKME: drop postinstall due run dev scripts during install --production
		# replace with fake commands?

		local COMMITMSG=''
Vitaly Lipatov's avatar
Vitaly Lipatov committed
631 632
                local PRODUCTION=''

Vitaly Lipatov's avatar
Vitaly Lipatov committed
633
		[ "$MODE" = "production" ] && PRODUCTION='--omit=dev'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
634

635
		info "Detected npm install hook, running ..."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
636
		docmd npm install $VERBOSE --omit=optional --ignore-scripts $PRODUCTION || fatal
Vitaly Lipatov's avatar
Vitaly Lipatov committed
637
		COMMITMSG="update node_modules with npm install $PRODUCTION for $VERSION (see $SDNAME in .gear/rules)"
638

639
		if [ -s $RGD/.gear/predownloaded-postinstall-hook ] ; then
640 641
		        info "Detected .gear/predownloaded-postinstall-hook, running it ..."
			$RUNHOOK $RGD/.gear/predownloaded-postinstall-hook $MODE $VERSION
642
		fi
643

644
		# prune removes modules not listed in package.json
Vitaly Lipatov's avatar
Vitaly Lipatov committed
645
		# a= npm prune $PRODUCTION
Vitaly Lipatov's avatar
Vitaly Lipatov committed
646 647
		# dedup restores removed modules!
		#docmd npm dedup
648 649

		info "Removing binaries ..."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
650 651 652 653 654
		if [ -d vendor ] ; then
			find vendor -name "*.a" -type f -delete -print
			find vendor -name "*.so" -type f -delete -print
			find vendor -name "*.dll" -type f -delete -print
		fi
655

656 657
		# drop all exclude node_modules
		# TODO: correct .* removing
658
		rm -rf $(ls -1 | grep -v node_modules) .[a-zA-Z0-9]*
659

660 661
		# remove build related modules we have in system
		#if [ -d node_modules/node-gyp/ ] ; then
662
		rm -rfv node_modules/{npm,node-gyp}/ node_modules/.bin/{npm,npx,node-gyp}
663 664 665
		#	ln -s /usr/lib/node_modules/node-gyp node_modules/
		#fi

666 667
		#epm assure jq || fatal
		#(cd node_modules && rm -rf $(jq -r -c '.devDependencies | keys[]' ../package.json))
668
	fi
669
        ### end of npm part
670

Vitaly Lipatov's avatar
Vitaly Lipatov committed
671 672
	# some modules contains own .git
	find -type d -name ".git" -print -exec rm -rvf "{}" \;
673
	popd
674 675

	docmd git add -f $PSM
Vitaly Lipatov's avatar
Vitaly Lipatov committed
676
	docmd git commit -m "$COMMITMSG"
677 678 679

}

680

681 682 683 684 685 686 687 688 689 690 691
will_commit()
{
    if [ -z "$GSSETVERSION" ] && [ -z "$FORCEDOWNLOAD" ]; then
        echog "Skip $FTB committing (run without new version or without -f)"
        return 1
    fi
    return 0
}



692
parse_cmd_pre_spec "$@"
693 694
mygetopts $LISTARGS

695
test -z "$VERBOSE" || echo "'$LISTNAMES' @ '$LISTRPMARGS'"
696

697 698 699
if [ -n "$LISTRPMARGS" ] ; then
	if [ -z "${LISTRPMARGS/*spec/}" ] ; then
		fatal "run with incorrect filename $LISTRPMARGS"
700
	fi
701
	if [ ! -f "$LISTNAMES" ] ; then
702 703
		fatal "set version permitted only for one file"
	fi
704
	if [ "${LISTRPMARGS/ /}" != "$LISTRPMARGS" ] ; then
705 706
		fatal "you run rpmgs with more than one version"
	fi
707
	GSSETVERSION=$LISTRPMARGS
708 709
fi

710 711
test -z "$LISTNAMES" && print_error

712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
[ -z "$GSSETRELEASE" ] || GSSKIPADDCHANGELOG=1

for spec in $LISTNAMES
do
	if [ -n "${spec/*spec/}" ] ; then
		print_error
	fi

	DOWNLOADSOME=''
	set_specdir $spec

# Answer me:
# for empty GSSETVERSION we do autoupdate to the latest version
# or it is a reason to retrieve the latest version from spec?

# firstly try modern autoupdate if version is empty
728
if is_gear $SPECDIR && [ -z "$GSSETVERSION" ] && $EPMCMD assure gear-rules-verify perl-Gear-Rules ; then
729 730
    # need for gear-rules-verify
    cd $(get_root_git_dir)
731

732 733 734
    GEAR_RULES_VERIFY=gear-rules-verify
    docmd $GEAR_RULES_VERIFY
    if $GEAR_RULES_VERIFY 2>&1 | grep -q "gear-rules-verify should be run in clean repository" ; then
735
        warning "gear-rules-verify should be run in clean repository, skipping"
736
    elif $GEAR_RULES_VERIFY | grep -q "Ready for tarball update" ; then
737
        #[ -n "$GSSETVERSION" ] && warning "we will ignore version and update to the latest version from watch file"
738
        # TODO: rpm-uscan 0.20.2.17.11 or above
739 740
        $EPMCMD assure rpm-uscan
        $EPMCMD assure gear-uupdate
741 742
        if ls $(get_root_git_dir)/.gear | grep \.watch$ || ls $(get_root_git_dir) | grep \.watch$ ; then
            docmd rpm-uscan -v --force-action gear-uupdate
743 744 745 746 747 748 749

            source_postupdate_hook

            # TODO: make plugins
            update_predownloaded development
            update_predownloaded production
            DOWNLOADSOME=1
750
        else
Vitaly Lipatov's avatar
Vitaly Lipatov committed
751
            warning "there are no .watch files in $(get_root_git_dir)/.gear, skip rpm-uscan"
752
        fi
753
    elif $GEAR_RULES_VERIFY | grep -Eq "Ready for external VCS update" ; then
754 755 756 757 758 759 760 761
        #[ -n "$GSSETVERSION" ] && warning "we will ignore version and update to the latest version from watch file"
        # TODO: rpm-uscan 0.20.2.17.11 or above
        # https://www.altlinux.org/Gear/remotes
        $EPMCMD assure gear-remotes-uscan perl-Gear-Remotes
        $EPMCMD assure gear-commit gear
        docmd gear-remotes-restore
        docmd gear-remotes-uscan
        docmd gear-commit
762 763 764 765 766 767 768 769

        # update_post_git_submodules
        source_postupdate_hook

        # TODO: make plugins
        update_predownloaded development
        update_predownloaded production
        DOWNLOADSOME=1
770
    else
771
        warning "have no version, but skipped gear-rules-verify (Check if you need run gear-remotes-save)"
772 773 774
    fi
fi

775 776 777
	# TODO: some duplication
	# secondly check for new manner use separated upstream branch
	# Note: gear-uupdate don't want work if package version is already changed
778 779 780
	if is_gear $SPECDIR && [ -z "$DOWNLOADSOME" ] && [ -s $(get_root_git_dir)/.gear/upstream/remotes ] && $EPMCMD assure gear-remotes-restore perl-Gear-Remotes ; then
		# need for gear-rules-verify
		cd $(get_root_git_dir)
781 782 783
		$EPMCMD assure gear-commit gear
		$EPMCMD assure gear-uupdate
		docmd gear-remotes-restore
784
		docmd git fetch --tags upstream
785 786 787 788 789 790 791 792 793
		# hack: if separated branch
		#if grep -q "^diff:" $(get_root_git_dir)/.gear/rules ; then
		#	update_master_branch_to "$GSSETVERSION"
		#else
		[ -n "$GSSETVERSION" ] || fatal "GSSETVERSION is empty here"
		tag="$(get_tag_by_version "$GSSETVERSION")"
		[ -n "$tag" ] || fatal "Can't find appropriate tag for $GSSETVERSION version"
		docmd gear-uupdate --upstream-version "$GSSETVERSION" --tag $tag
		docmd gear-commit
794

795 796
		# update_post_git_submodules
		source_postupdate_hook
797

798 799 800 801 802 803
		# TODO: make plugins
		update_predownloaded development
		update_predownloaded production
		DOWNLOADSOME=1
		#fi
	fi
804

805
	# Set version if needed
806
	if [ -n "$GSSETVERSION" ] && [ "$GSSETVERSION" != "HEAD" ] ; then
807
		CURVER=$(get_version $spec)
808
		set_version $spec ${GSSETVERSION/-*/}
809
		if [ "$CURVER" != "$GSSETVERSION" ] ; then
810
			set_release $spec $GSSETRELEASE
Vitaly Lipatov's avatar
Vitaly Lipatov committed
811 812 813 814 815
			if [ -n "$GSSETRELEASE" ] ; then
				echo "Set new $GSSETVERSION-$GSSETRELEASE version for $spec"
			else
				echo "Set new $GSSETVERSION version for $spec"
			fi
816
		else
817
			echo "Version $GSSETVERSION already is set"
818
			GSSKIPADDCHANGELOG=1
819 820
		fi
	fi
821

822
	# download from all possible sources
823
	LOADALL=1
824 825 826 827 828
	if [ -n "$LOADALL" ] ; then
		SOURCELIST=$(grep "^Source[0-9]*:" $spec | sed -e "s|:.*||g")
	else
		SOURCELIST=$(grep "^Source0\?:" $spec | sed -e "s|:.*||g")
	fi
829

830
    FIRSTSOURCEDIR=''
831
	if [ -z "$DOWNLOADSOME" ] ; then
832 833
	for SN in $SOURCELIST
	do
834
		CURRENTURL=''
835
		GETSOURCE=$(eval_spec $spec | get_var "$SN")
836 837
		[ -n "$GETSOURCE" ] || fatal "Problem with empty $SN"

Vitaly Lipatov's avatar
Vitaly Lipatov committed
838
		echo "Updating $SN: $GETSOURCE ..."
839

840
		# for get RPMSOURCEDIR and BASENAME/VERSION/RELEASE
841
		build_rpms_name $spec
842

843 844 845
		mkdir -p $RPMSOURCEDIR/ || fatal "Can't create/chdir..."
		FTB=$RPMSOURCEDIR/$(basename "$GETSOURCE")

846
		# TODO: do not use RPMSOURCEDIR for temp. tarballs
847
		[ -n "$FORCEDOWNLOAD" ] && rm -fv "$FTB"
848
		#[ -f "$RPMSOURCEDIR/$FTB" ] && { echog "Tarball $FTB already exists in $RPMSOURCEDIR dir, skipping." ; continue ; }
849

850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
		if rhas "$GETSOURCE" "ps?://" ; then
			download_any_tarball "$GETSOURCE" "$FTB"
			will_commit || continue
			CURRENTURL="${GETSOURCE}"
			if is_gear ; then
				commit_tarball "$FTB" || fatal
				source_postupdate_hook
			fi
			DOWNLOADSOME=1
			echog "DONE with $FTB"
			continue
		fi

		# Test for eterbuild extensions like # Source-git
		# (will set GETSOURCEURL or GETSOURCESVN)
865
		get_source_url $spec $SN || get_source_git $spec $SN || get_source_svn $spec $SN || get_source_script $spec $SN || get_source_vcs $spec $SN
866
		#[ "$SN" = "Source1" ] && exit
867 868 869
		#if ! rhas "$GETSOURCE" ".tar$" ; then
		#	warning "It is recommended to use .tar tarballs for sources ($FTB now)"
		#fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
870

871 872
		if [ -n "${GETSOURCESVN}" ] ; then
			is_gear $SPECDIR || fatal "Source-svn works only with gear repo"
873 874 875 876 877 878 879
			will_commit || continue
			# clone svn repo to current dir
			# FIXME: need to clone in git root dir
			docmd git svn clone $GETSOURCESVN $(get_root_git_dir)
			echo "Run svn rebase from $GETSOURCESVN"
			docmd git svn rebase
			DOWNLOADSOME=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
880

881 882
		elif [ -n "${GETSOURCEGIT}" ] ; then
			is_gear $SPECDIR || fatal "Source-git works only with gear repo"
883 884 885 886 887 888 889 890 891 892
			will_commit || continue
			echog "Try to fetch ${GETSOURCEGIT} for $spec"
			#TODO error if incompatible
			docmd git remote add upstream $GETSOURCEGIT
			docmd git fetch --tags upstream
			update_master_branch_to "$GSSETVERSION"

			update_post_git_submodules
			source_postupdate_hook
			DOWNLOADSOME=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
893

894 895 896 897
		elif [ -n "${GETSOURCESCRIPT}" ] ; then
			is_gear $SPECDIR || fatal "Source-script works only with gear repo"
			#[ -x "$GETSOURCESCRIPT" ] || fatal "Can't find executable $GETSOURCESCRIPT"
			bash -x $(get_root_git_dir)/$GETSOURCESCRIPT $GSSETVERSION "$FTB" || fatal "fatal with $GETSOURCESCRIPT"
898
			will_commit || continue
899
			CURRENTURL="${GETSOURCESCRIPT}"
900 901
			commit_tarball "$FTB" || fatal
			source_postupdate_hook
902 903
			DOWNLOADSOME=1

Vitaly Lipatov's avatar
Vitaly Lipatov committed
904 905
		# TODO: rewrite code to use original file format and temp. download dir
		elif [ -n "${GETSOURCEURL}" ] ; then
906
			if [ -z "$FORCEDOWNLOAD" ] && check_tarball "$FTB" ; then
907
				echog "$FTB already exists, skipping... "
908 909
			else
				echog "Try to load ${GETSOURCEURL} for $spec"
910
				download_to "$GETSOURCEURL" "$FTB" || fatal "Can't download $GETSOURCEURL"
911
				will_commit || continue
912
				CURRENTURL="${GETSOURCEURL}"
913 914 915 916
				if is_gear ; then
					commit_tarball "$FTB" || fatal
					source_postupdate_hook
				fi
917
				DOWNLOADSOME=1
918
			fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
919

920 921
		else
			warning "$SN $GETSOURCE has no URL. Skipping... "
922 923
		fi

924
	done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
925 926 927 928 929

	# TODO: make plugins
	update_predownloaded development
	update_predownloaded production

930
	fi
931

932
	[ -n "$DOWNLOADSOME" ] || fatal "No upstream code is updated for new version $GSSETVERSION."
933

934 935 936 937 938 939
	if [ -z "$GSSKIPADDCHANGELOG" ] ; then
		# Write changelog if all done
		CURVER=$(get_version $spec)
		CURREL=$(get_release $spec)
		EGEARME=""
		is_gear && EGEARME=" with rpmgs script"
940
		if grep -q "^- new version $CURVER$" $spec ; then
941
			subst "s|- new version $CURVER$|- new version ($CURVER)$EGEARME via gear-uupdate|" $spec && git commit --amend $spec
942 943 944
		else
			add_changelog_helper "- new version ($CURVER)$EGEARME" $spec || echog "Changelog entry for $CURVER-$CURREL already exists"
		fi
945
	fi
946

947 948 949 950
done

exit 0