Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
antSword
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
huangjunbo
antSword
Commits
4d20e320
Commit
4d20e320
authored
Jun 01, 2016
by
antoor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rewriting program update feature
重写程序更新功能
parent
2e00deca
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
106 additions
and
112 deletions
+106
-112
update.js
modules/update.js
+34
-112
style.css
static/update/style.css
+44
-0
update.js
static/update/update.js
+7
-0
update.html
views/update.html
+21
-0
No files found.
modules/update.js
View file @
4d20e320
//
/**
// 程序更新模块
* 中国蚁剑::更新程序
//
* 开写: 2016/05/31
/* 更新流程:
*/
-------
1. 获取远程github上的package.json信息
2. 和本地版本进行判断,不一致则提示更新
3. 下载用户选择的更新源文件到临时目录`.antSword-{now}`
4. 替换程序中的`resources/app.asar`文件
5. 提示用户手动重启,关闭应用
*/
'use strict'
;
const
config
=
require
(
'./config'
);
const
electron
=
require
(
'electron'
);
const
os
=
require
(
'os'
),
const
BrowserWindow
=
electron
.
BrowserWindow
;
fs
=
require
(
'fs'
),
path
=
require
(
'path'
),
unzip
=
require
(
'extract-zip'
),
crypto
=
require
(
'crypto'
),
nugget
=
require
(
'nugget'
),
// logger = require('log4js').getLogger('Update'),
// Logger = require('./logger'),
// logger = null,
superagent
=
require
(
'superagent'
);
let
logger
;
class
Update
{
class
Update
{
constructor
()
{
constructor
(
electron
)
{
this
.
listenHandler
();
logger
=
new
electron
.
Logger
(
'Update'
);
this
.
openWindow
();
const
ipcMain
=
electron
.
ipcMain
;
this
.
info
=
{};
ipcMain
.
on
(
'update-check'
,
(
event
,
arg
)
=>
{
this
.
check
(
arg
[
'local_ver'
],
(
hasUpdate
,
retVal
)
=>
{
logger
.
debug
(
'check-result'
,
hasUpdate
,
retVal
);
event
.
sender
.
send
(
'update-check'
,
{
hasUpdate
:
hasUpdate
,
retVal
:
retVal
});
});
})
.
on
(
'update-download'
,
(
event
,
source
)
=>
{
logger
.
debug
(
'update-download'
,
source
);
const
info
=
this
.
info
[
'update'
];
const
downloadUrl
=
info
[
'sources'
][
source
];
this
.
download
(
downloadUrl
,
info
[
'md5'
],
(
done
,
retVal
)
=>
{
event
.
sender
.
send
(
'update-download'
,
{
done
:
done
,
retVal
:
retVal
});
});
});
}
}
// 检查是否有更新
/**
// 参数{localVer: 本地版本号, callback: 回调函数(是否有更新, 是?更新信息:错误信息)}
* 事件监听器
check
(
localVer
,
callback
)
{
* @return {[type]} [description]
logger
.
debug
(
'check'
,
localVer
);
*/
superagent
listenHandler
()
{
.
get
(
'https://raw.githubusercontent.com/antoor/antSword/master/package.json'
)
electron
.
ipcMain
.
timeout
(
9527
)
.
on
(
'update-getVersion'
,
(
event
)
=>
{
.
end
((
err
,
res
)
=>
{
event
.
returnValue
=
config
.
package
[
'version'
]
if
(
err
)
{
return
callback
(
false
,
err
.
toString
())
};
})
try
{
const
info
=
JSON
.
parse
(
res
.
text
);
this
.
info
=
info
;
callback
(
info
[
'version'
]
!==
localVer
,
info
);
}
catch
(
e
)
{
return
callback
(
false
,
e
.
toString
());
}
});
}
}
// 下载更新
/**
// 参数{downloadUrl: 下载地址, md5: 校验MD5, callback: 回调(成功?(true, null):(false, err))}
* 打开更新窗口
download
(
downloadUrl
,
md5
,
callback
)
{
* @return {[type]} [description]
// 创建临时文件
*/
const
tmpDir
=
os
.
tmpDir
();
openWindow
()
{
const
fileName
=
'.antSword-'
+
(
+
new
Date
);
let
win
=
new
BrowserWindow
({
const
tmpFileName
=
path
.
join
(
tmpDir
,
fileName
);
width
:
400
,
// 当前目录环境
height
:
250
,
const
curDir
=
path
.
join
(
__dirname
,
'../../'
);
// height: 180,
// 开始下载文件
// resizable: false,
nugget
(
minimizable
:
false
,
downloadUrl
,
maximizable
:
false
{
target
:
fileName
,
dir
:
tmpDir
,
resume
:
true
,
verbose
:
true
,
strictSSL
:
downloadUrl
.
startsWith
(
'https'
)
},
(
err
)
=>
{
if
(
err
)
{
return
callback
(
false
,
err
.
toString
())
};
// 校验MD5
const
_md5
=
crypto
.
createHash
(
'md5'
).
update
(
fs
.
readFileSync
(
tmpFileName
)).
digest
(
'hex'
);
if
(
_md5
!==
md5
)
{
return
callback
(
false
,
{
type
:
'md5'
,
err
:
_md5
})
};
// ZIP解压
unzip
(
tmpFileName
,
{
dir
:
tmpDir
},
(
e
)
=>
{
if
(
e
)
{
return
(
callback
(
false
,
{
type
:
'unzip'
,
err
:
e
}))
};
// 删除旧asar
// fs.unlinkSync(path.join(curDir, 'app.asar'));
// 移动新asar
fs
.
rename
(
path
.
join
(
tmpDir
,
'antSword.update'
),
path
.
join
(
curDir
,
'app.asar'
),
(
_e
)
=>
{
_e
?
callback
(
false
,
_e
.
toString
())
:
callback
(
true
);
}
);
});
});
win
.
loadURL
(
'ant-views://update.html'
);
win
.
webContents
.
openDevTools
();
}
}
);
}
}
}
module
.
exports
=
Update
;
module
.
exports
=
Update
;
static/update/style.css
0 → 100644
View file @
4d20e320
html
,
body
,
#container
{
width
:
100%
;
height
:
100%
;
margin
:
0
;
padding
:
0
;
overflow
:
hidden
;
}
#container
{
display
:
flex
;
align-items
:
center
;
}
#left
{
height
:
100%
;
padding
:
10px
;
}
#left
>
img
{
height
:
100%
;
}
#right
{
border-left
:
1px
solid
rgba
(
158
,
158
,
158
,
0.33
);
padding-left
:
10px
;
}
#name
{
font-size
:
26px
;
margin
:
10px
0
;
color
:
#009688
;
font-family
:
sans-serif
;
}
#version
{
font-size
:
14px
;
font-family
:
sans-serif
;
color
:
#9E9E9E
;
}
#status
{
font-size
:
14px
;
font-family
:
sans-serif
;
color
:
#00BCD4
;
margin
:
5px
0
;
}
static/update/update.js
0 → 100644
View file @
4d20e320
'use strict'
;
const
electron
=
require
(
'electron'
);
const
ipcRenderer
=
electron
.
ipcRenderer
;
// 获取版本号
document
.
querySelector
(
'#version'
).
innerText
=
'v'
+
ipcRenderer
.
sendSync
(
'update-getVersion'
);
views/update.html
0 → 100644
View file @
4d20e320
<!DOCTYPE HTML>
<html>
<head>
<meta
charset=
"utf-8"
/>
<title>
Update
</title>
<link
rel=
"stylesheet"
href=
"ant-static://update/style.css"
/>
</head>
<body>
<div
id=
"container"
>
<div
id=
"left"
>
<img
src=
"ant-static://imgs/logo.png"
/>
</div>
<div
id=
"right"
>
<div
id=
"name"
>
AntSword
</div>
<div
id=
"version"
>
loading..
</div>
<div
id=
"status"
>
Checking..
</div>
</div>
</div>
</body>
<script
src=
"ant-static://update/update.js"
></script>
</html>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment