Sparkle 常规使用姿势

macOS Jan 23, 2017

Maria 在最近终于添加上了自动更新的功能,之前有想过加这个功能,但是当进入 Sparkle 官网看到密密麻麻的文档和需要手动签证书什么的字眼,懒癌突然发作。

寒假一天闲着总算是把这个功能添加上了。总的来讲,添加这个功能非常的简单,但前提是你使用较新版本的 Xcode,第二前提是你有耐心仔细的看使用文档[1](这一点特别重要,不然你会犯一些特别弱智的错误)。

appcast.xml

Sparkle 发布更新是通过 RSS 的方式来做的。Sparkle 自定义了一套专用的 xml 格式,如果手动书写的话会比较麻烦,所以自己写了一些小程序来快速生成 appcast.xml。

// npm install xml
var xml = require('xml');

var Appcast = function(info) {
  this.title = info.title
  this.description = info.description
  this.language = info.language
  this.items = []
}

Appcast.prototype.addItem = function(item) {
  this.items.push({
    "item": [{
      "title": item.title,
    },{
      "pubDate": item.pubDate,
    },{
      "sparkle:releaseNotesLink": item.releaseNotesLink,
    },{
      "enclosure": [{
        "_attr": {
          "url": item.url,
          "sparkle:version": item.version,
          "sparkle:shortVersionString": item.shortVersionString,
          "type": "application/octet-stream",
          "sparkle:dsaSignature": item.dsaSignature,
        }
      }],
    },{
      "sparkle:minimumSystemVersion": "10.8"
    }]
  })
};

Appcast.prototype.xml = function(options) {
  var cast = {
    "rss": [{
      "_attr": {
        "version": "2.0",
        "xmlns:sparkle": "http://www.andymatuschak.org/xml-namespaces/sparkle",
        "xmlns:dc": "http://purl.org/dc/elements/1.1/"
      }
    },{
      "channel": [{
        "title": "Maria's Changelog",
      },{
        "description": "Most recent changes with links to updates.",
      },{
        "language": "en",
      }]
    }]
  }

  this.items.forEach(function(item) {
    cast.rss[1].channel.push(item)
  });
  
  var xmlString = xml(cast, { 
    declaration: true,
    indent: true
  });
  return xmlString;
};


module.exports = Appcast

使用示例:

var Appcast = require('./appcast.js')

cast = new Appcast({
  title: "Maria's Changelog",
  description: "Most recent changes with links to updates.",
  language: "en"
})

cast.addItem({
  title: "Version 1.2.6",
  pubDate: "Wed, 09 Jan 2006 19:20:11 +0000",
  releaseNotesLink: "http://proj.windisco.com/Maria/Maria_1.2.6.html",
  version: "1701214",
  shortVersionString: "1.2.6",
  url: "http://proj.windisco.com/Maria/Maria_1.2.6.zip",
  dsaSignature: "",
  minimumSystemVersion: "10.8"
})

console.log(cast.xml());

DSA Signature

签证是可以自己手动签的,一开始我是觉得自己签很麻烦(对我就是这么的懒),给 Maria 上自动更新的功能也是拖了很久,最近几天下定决心做好这事之后好好看了看文档,发现 Sparkle 项目文件夹里就提供好了生成证书的二进制程序 ./bin/generate_keys。生成的证书分为公钥和私钥(默认名字为 dsa_priv.pemdsa_pub.pem)。公钥放进 Xcode 项目文件文件夹里就行了,私钥则是用来生成密钥供每次发布更新时填进 appcast.xml

./bin/sign_update path_to_your_update.zip path_to_your_dsa_priv.pem

Draft


  1. https://sparkle-project.org/documentation/ ↩︎

Tags

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.